wugology
wugology

Reputation: 193

Using csv column names as parameter in R function

I'm trying to write a function that will open some .csv files and preform several calculations on specific parts of those dataframes. I'm having trouble passing a column name as a parameter in the function, and I'm not sure why. So something like this (minimal working example, my function is more complex than this):

MyFunction <- function(file, columnname){
    data <- read.table(file,sep=",",header=TRUE);
    mean(data$columnname);
    }

I have a .csv in my desktop called "test.csv", and all it has in it is a column called "numbers" and the numbers 1:10. If I run either of these:

MyFunction("~/Desktop/test.csv",numbers)
MyFunction("~/Desktop/test.csv","numbers")

I get this error:

[1] NA
Warning message:
In mean.default(data$columnname) :
   argument is not numeric or logical: returning NA

However, if I run this:

data <- read.table("~/Desktop/test.csv",sep=",",header=TRUE);
mean(data$numbers);

I get this:

[1] 5.5

... which is what I want.

I'm not sure how my function is different from doing it by hand here. I can use function parameters to find and open the file, but using the function parameter in the data$parameter seems to be causing an error. Why is this? What workaround is there?

Upvotes: 0

Views: 953

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 270195

Try this:

MyFunction <- function(file, columnname) {
    data <- read.csv(file)
    mean(data[[columnname]])
}

Note:

b <- "a"
DF <- data.frame(a = 1, b = 2)
DF$b
## [1] 2
DF[[b]]
## [1] 1

Upvotes: 4

Related Questions