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

后端 未结 6 750
难免孤独
难免孤独 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 01:55

    Any of the following should work!!

    df <- data.frame(x=1:3,y=4:6)
    
    mean(df$x)
    mean(df[,1])
    mean(df[["x"]])
    

提交回复
热议问题