Subset dataframe based on column value using a function in R

十年热恋 提交于 2019-12-13 00:57:10

问题


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,]

回答1:


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]




回答2:


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



来源:https://stackoverflow.com/questions/34076956/subset-dataframe-based-on-column-value-using-a-function-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!