简体   繁体   中英

How to subset data frame based on user specified conditions

I have a data frame contains three different loan population (A , B and C). Each loan population have two subgroups (NR, RF). The data frame also has loan level year and risk type information.

I need to write function to subset the data frame based on user define parameter. The use may choose to run the entire loan population or select individual population, subgroups, risk type or any combination of these variables. for example: use might want to subset only pop = 'A' or subset only pop = 'C' & year == 2001 or risk_type == 3.

can someone please suggest methods I can accomplish this task.

Here is sample data;

df <- data.frame(id=c(1:10),
         pop=c(rep('A',3),rep('B',3),rep('C',4)),
         subgroup=c('NR','NR','RF','RF','NR','RF','NR','NR','RF','NR'),
         year=c(2000,2001,2002,2000,2001,2002,2000,2001,2002,2002),
         risk_type=c(1,1,3,2,2,3,1,3,2,1)
         )

You can just do a simple wrapper over dplyr filter:

library(dplyr)
library(lazyeval)

df_subset_ = function(condition)
  df %>% filter_(.dots = condition)

df_subset = function(condition)
  df_subset_(lazy(condition))

df_subset(pop == 'C' & year == 2001)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM