Mean of a column in a data frame, given the column's name

后端 未结 6 760
难免孤独
难免孤独 2020-11-30 01:13

I\'m inside a big function I have to write. In the last part I have to calculate the mean of a column in a data frame. The name of the column I am operating on is given as a

6条回答
  •  感情败类
    2020-11-30 02:02

    I think you're asking how to compute the mean of a variable in a data frame, given the name of the column. There are two typical approaches to doing this, one indexing with [[ and the other indexing with [:

    data(iris)
    mean(iris[["Petal.Length"]])
    # [1] 3.758
    mean(iris[,"Petal.Length"])
    # [1] 3.758
    mean(iris[["Sepal.Width"]])
    # [1] 3.057333
    mean(iris[,"Sepal.Width"])
    # [1] 3.057333
    

提交回复
热议问题