S******y 发帖数: 1123 | 1 I have a function which takes in two arguments -
1) data : input data frame
2) x: a column name
But within function, it seems that I cannot use data$x to properly
reference an existing column within my data frame.
For example,
> library(MASS)
> length(iris$Species)
[1] 150
> fx<-function(data, x) {
+ print(length(data$x))
+ }
>
#hope to return 150. but none of the following is working right!!!!
> fx(iris,Species)
[1] 0
> fx(iris,'Species')
[1] 0 | m********n 发帖数: 11 | 2 try data[x] where x is a vector of characters | S******y 发帖数: 1123 | 3 Thanks! Got it -
> fx<-function(data, x) {
+ print(length(data[[which(names(data)==x)]]))
+ }
> fx(iris,'Species')
[1] 150 |
|