简体   繁体   中英

Passing column name as parameter to function in R language

I have got a function :

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

Please guide further. Thanks:)

you need to use another way of accessing the columns in the dataframe than via $ . There are two other ways:

1.

print(content[[param]])

2.

print(content[,param])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM