2012年3月29日星期四

a good lapply() tutorial



Using lapply() to Change Multiple Dataframes with One Call


# a dataframe
a <- data.frame(x = 1:3, y = 4:6)
 
# make a list of several dataframes, then apply function (change column names, e.g.):
my.list <- list(a, a)
my.list <- lapply(my.list, function(x) {names(x) <- c("a", "b") ; return(x)})
 
# save dfs to csv with similar lapply-call:
n <- 1:length(my.list)
lapply(n, function(ni) {
               write.table(file = paste(c(1:length(my.list))[ni], ".csv", sep = ""), 
               my.list[ni], sep = ";", row.names = F)
               }
       )

没有评论:

发表评论