如何使用字符串函数参数来对该函数中的数据框进行子集化

时间:2016-11-11 03:50:35

标签: r

我想编写一个可重用的函数,其输入参数将是数据框,以及我希望将其子集化的列的名称。

该功能定义如下:

funct <- function(df, colnames){
  df_subset = df[ , colnames]
   return(df_subset) }

flights_subset <- funct(flights, c("MONTH","YEAR") ) #1st arg is a df, 2nd arg is a string

为了更清晰,我已经给出了代码来创建输入文件&#39; flight&#39;以及我期望从函数&#39; flights_subset&#39;

中返回的文件
flights <- structure(list(YEAR = c(2011L, 2011L, 2011L, 2011L, 2011L), MONTH = c(1L, 
                                                                             1L, 1L, 1L, 1L), DAYOFMONTH = 1:5, DAYOFWEEK = c(6L, 7L, 1L, 
                                                                                                                              2L, 3L), DEPTIME = c(1400L, 1401L, 1352L, 1403L, 1405L)), .Names = c("YEAR", 
                                                                                                                                                                                                   "MONTH", "DAYOFMONTH", "DAYOFWEEK", "DEPTIME"), row.names = 5424:5428, class = "data.frame")

flights_subset <- structure(list(MONTH = c(1L, 1L, 1L, 1L, 1L), YEAR = c(2011L, 
                                                                     2011L, 2011L, 2011L, 2011L)), .Names = c("MONTH", "YEAR"), class = "data.frame", row.names = 5424:5428)

1 个答案:

答案 0 :(得分:1)

您的版本没问题,您只需要修复function中的拼写错误。此外,不需要返回声明。

funct <- function(df, colnames){
   df[ , colnames] 
}

或者您可以使用dplyr

library(dplyr)   

funct <- function(df, colnames){
df %>% select_(colnames)
 }


flights_subset <- funct(flights, c("MONTH", "YEAR"))