Loop through multiple columns in R -


i have following code i'd run multiple columns in data frame called ccc.

ccc %>%    group_by(la) %>%   summarise(def = sum(defaultoct05 == 'def'),             ndef = sum(defaultoct05 != 'def'),             drate = mean(defaultoct05 == 'def')) 

la name of 1 of columns. how set loop run through number of different columns?

i've tried following.

for (i in 26:ncol(ccc)) {     ccc %>%          group_by(i) %>%         summarise(def = sum(defaultoct05 == 'def'),                   ndef = sum(defaultoct05 != 'def'),                   drate = mean(defaultoct05 == 'def')) } 

but following error message.

error in resolve_vars(new_groups, tbl_vars(.data)) : unknown variable group : i

what people miss in question reproducible data set. without it, hard reproduce problem , solve it.

if got right, data-set looks 1 above:

set.seed(1) ccc=data.frame(default=sample(c(0,1),100,replace = true),la=sample(c("x","y","z"),100,replace = true),dc=sample(c("a","b","c"),100,replace = true)) 

do.call() - applies rbind() subsequent elements. lapply(dat,function(x)) applies function every element of dat - in our case columns.

library(dplyr) do.call(rbind,lapply(ccc, function(var) {     dat=data.frame(var,default=ccc$default) %>% group_by(var) %>% summarise(def=sum(default),ndef=n()-sum(default),drate=mean(default))   return(as.data.frame(dat))   } )) 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -