Subset dataframe based on column value using a function in R

I am trying to subset a dataframe with the following function.

SubsetDF <- function(DF, VAR, YEAR){
   DF2 <- DF[DF$VAR <= YEAR, ]
   return(DF2)
 }

test <- SubsetDF(myData, "YEAR", 2000)

The resulting "test" is empty. What am I missing here? By the way, if I just do below, then the resulting dataframe is fine.

 myData[myData$YEAR <= 2010,]

Answers


Try

SubsetDF <- function(DF, VAR, YEAR){
   DF2 <- DF[DF[VAR] <= YEAR, ]
   return(DF2)
 }

test <- SubsetDF(myData, "YEAR", 2000)

Just replace the DF$VAR part with DF[VAR]


An approach using non-standard evaluation

d <- data.frame(YEAR = 1996:2005, x = 1:10)

test <- function(df, name, year){
  df[eval(substitute(name), df) <= year, ]
}

test(d, YEAR, 2000)

Notice that the year variable is not passed as a character (this is what is meant by non-standard evaluation). For more on non-standard evaluation read this http://adv-r.had.co.nz/Computing-on-the-language.html


Need Your Help

Excel VBA Highlight duplicates in active column

excel-vba vba excel

I'm trying to create a macro that will highlight duplicates in the column where text is being entered.

simplify and rearrange my google script for faster process this often stucks in the middle of the process

google-sheets google-apps-script

Can anyone simplify and rearrange my script such that it works faster without stucking . This script was made to extract spreadsheet data from 4 spreadsheets and deleting blank rows based on a column