Extracting data.frame column using argument? Not working?

Extracting data.frame column using argument? Not working?

by Edgardo Torres -
Number of replies: 4

Hey guys, quick question.

I swear I have done it before, but it isn't working for some reason this time, perhaps you guys can help me with the syntax:

I am interested in extracting a column from a data.frame ("df"). However, the name of the column to be extracted will be provided by an argument. It looks a little like this:

extraction <- function (x) {

column.in.question <- df$x

print(column.in.question)

}

In this scenario, executing "extraction(Glutamate)" should extract the column called "Glutamate" from the data frame "df", however, it does not. It returns "NULL".

If I manually type "df$Glutamate", it magically works again.  This leads me to believe that somethings about using the argument as a column name isn't translating properly, even though I have done this exact process before. 

I would greatly appreciate any help.

In reply to Edgardo Torres

Re: Extracting data.frame column using argument? Not working?

by Edgardo Torres -
This problem is also affecting other parts of my code, for example:

if I have a vector args <- "Glutamate"

shapiro.test(df$Glutamate) works, but shapiro.test(df$args) OR shapiro.test(df[args]) does not!
In reply to Edgardo Torres

Re: Extracting data.frame column using argument? Not working?

by Akash Kothari -
Hi Edgardo,

Did you assign df as a global variable? If you did something similar to the last assignment with a load data function, the dataframe is only stored within that function.
If you want to do extraction(Glutamate), I think you need to load the data into the function first..

Hopefully that helps.

Akash
In reply to Akash Kothari

Re: Extracting data.frame column using argument? Not working?

by Edgardo Torres -
Yes, it was a global variable, the problem was something else, but I found a workaround. In case anyone is interested:

if you have a vector: argument <- "Glutamate"

For some reason, I can't recall the column for a data frame (df) like this: df$argument

However, you can recall a column from a data frame (df) like this: df[argument]

The problem with recalling the column with "[]" however, is that it will not work with something like the shapiro.test() function like I mentioned before. The reason for this, and something that wasn't mentioned in lecture 4 of recalling columns, is that the different ways to recall a column we went over are NOT equivalent as per the slide.

The way you would recall the column for it to work with the shapiro.test() function would be with double square brackets rather that single, like this: df[[argument]]

The reason for this is that "[]" recalls the column as a data frame and "[[]]" recalls the column as a vector. As to why I can't recall the column like this: df$argument , I would still be interested to know the answer if anyone has it.
In reply to Edgardo Torres

Re: Extracting data.frame column using argument? Not working?

by Erik Spence -
You can't recall the column that way due to an inconsistency in the way R treats column names. df$argument will look for the 'argument' column. R has no reason to think that argument is a variable.