str_rst
str_rst

Reputation: 175

parsing list of column names in function

I want to parse list of column of variables to a function dynamically, right now i am passing manually but I want parse dynamically .

I tried lst from dplyr but didn't work

I want to pass names of columns in lst like below and it should work . the output should be the same as working now

col_lst<-lst(qq, ss)

data<-data.frame( c1=c(1,1,NA,NA,NA,NA,NA,NA,1,NA,NA,NA,NA,NA,NA,NA,NA,1,NA,NA,NA,1,1,1,NA,1,1,NA,NA,NA,NA,1,NA,NA,NA,NA,1,NA,1),
                  c2=c(1,1,1,1,1,NA,NA,NA,NA,1,1,1,1,1,NA,NA,NA,1,1,1,NA,1,1,1,1,1,NA,NA,NA,1,1,1,1,1,1,1,NA,NA,NA),
                  c3=c(1,1,NA,NA,NA,NA,NA,1,NA,NA,NA,NA,NA,NA,NA,NA,1,NA,NA,NA,NA,NA,1,1,1,NA,NA,NA,1,NA,NA,1,1,1,1,1,NA,NA,1),
                  c4=c(1,NA,NA,NA,NA,NA,NA,NA,NA,NA,1,NA,NA,NA,NA,NA,NA,NA,1,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA),
                  c5=c(NA,1,NA,NA,1,NA,1,1,NA,NA,1,NA,1,1,NA,1,NA,1,1,NA,1,NA,NA,1,NA,NA,1,NA,1,NA,NA,1,NA,1,NA,1,NA,1,NA),
                  c6=c(1,NA,1,NA,NA,1,NA,NA,1,NA,1,NA,1,1,NA,1,1,NA,1,NA,1,1,NA,1,NA,1,1,1,1,1,1,1,NA,1,NA,1,1,NA,1),
                  c7=c(NA,1,1,NA,1,NA,1,1,NA,NA,1,1,NA,NA,NA,1,1,NA,1,NA,1,1,NA,1,NA,1,NA,1,NA,1,NA,1,NA,1,NA,NA,1,1,NA),
                  c8=c(NA,1,1,NA,1,NA,1,1,NA,NA,1,1,NA,NA,NA,1,1,NA,1,NA,1,1,NA,1,NA,1,NA,1,NA,1,NA,1,NA,1,NA,NA,1,1,NA),
                  region=c(1,2,1,1,1,2,1,2,2,1,2,NA,1,1,2,2,2,1,1,1,2,NA,2,1,1,1,2,2,2,NA,1,2,2,1,1,1,2,2,2)
)



df<- data
df_list <- df_lst(df,lst)

Upvotes: 0

Views: 106

Answers (2)

akrun
akrun

Reputation: 886938

We could use split.default to automatically create a list

df_list <- split.default(df, names(df) %in% c('qq', 'ss'))

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 388807

You can create a vector of column names and use that to subset from df.

lst <- c('qq', 'ss')
df_list <- df_lst(df,df[lst])

Upvotes: 0

Related Questions