【问题标题】:Passing column name as parameter to function in R language [duplicate]将列名作为参数传递给R语言中的函数[重复]
【发布时间】:2011-06-11 00:15:46
【问题描述】:

我有一个函数:

aggreg <- function(fileName, param){   
  contents <- read.csv(fileName, header=T)
  #print(contents)  #This displays all contents
  print(contents$param) #gives NULL
}

> aggreg("test.csv","Close.Price")
> NULL

请进一步指导。谢谢:)

【问题讨论】:

    标签: r


    【解决方案1】:

    您需要使用另一种方式来访问数据框中的列,而不是通过$。还有另外两种方式:

    1.

    print(content[[param]])
    

    2.

    print(content[,param])
    

    【讨论】:

    • 拯救了我的一天。 df[df[columnName] > 0, ]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 2020-01-13
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多