Overwriting result with for loop in R -
i have number of csv files , goal find number of complete cases file or set of files given id argument. function should return data frame column id specifying file , column obs giving number of complete cases id. however, function overwrites previous value of nobs in each loop , resulting data frame gives me last value. have idea how value of nobs each value of id?
myfunction<-function(id=1:20) { files<-list.files(pattern="*.csv") myfiles = do.call(rbind, lapply(files, function(x) read.csv(x,stringsasfactors = false))) (i in id) { good<-complete.cases(myfiles) newframe<-myfiles[good,] cases<-newframe[newframe$id %in% i,] nobs<-nrow(cases) } clean<-data.frame(id,nobs) clean } thanks.
we can inside lapply(), below (not tested):
myfunction <- function(id = 1:20) { files <- list.files(pattern = "*.csv")[id] do.call(rbind, lapply(files, function(x){ df <- read.csv(x,stringsasfactors = false) df <- df[complete.cases(df), ] data.frame(id=x,nobs=nrow(df)) } ) ) }
Comments
Post a Comment