r - How to split large dataframe by columns and write into individual CSVs -


i have large csv file new.dat 100s of column names. want split new.dat per column names keeping first column in new subset written .csv.

new.dat

new.dat <- structure(list(sequence = c("aaaaaacctgttctgata", "aaaaaaggctgttactgagc",  "aaaaacattcgagcgagatctct", "aaaaacctcgacttcggaag", "aaaaagctcgtagttgaa",  "aaaaagctcgtagttgaac"), wt1 = c("84", "104", "80", "35", "112",  "350"), wt2 = c("149", "478", "502", "186", "577", "911"), ago1 = c("32",  "147", "433", "51", "258", "353"), ago2 = c("37", "222", "355",  "85", "408", "420"), dcl1 = c("56", "185", "291", "48", "167",  "273"), dcl2 = c("59", "176", "294", "31", "185", "245"), nas = c(0l,  0l, 0l, 0l, 0l, 0l)), .names = c("sequence", "wt1", "wt2", "ago1",  "ago2", "dcl1", "dcl2", "nas"), row.names = c(na, 6l), class = "data.frame") 

so result new.dat data should have 7 csv files. first csv wt1.csv sequence , wt1 columns, second csv file wt2.csv sequence , wt2 columns , forth..

this code have tried. please suggest missing here. thanks

for (name in colnames(new.dat[-1])){    tmp=subset(new.dat$sequence, colnames==name)    fn= name    #save csv file     write.csv(tmp,fn,row.names=false)  } 

we can loop on column names except first 1 lapply, subset columns of dataset including 'sequence' column , write file

lapply(names(new.dat)[-1], function(nm)     write.csv(new.dat[c("sequence", nm)],         paste0(nm, ".csv"), quote = false, row.names = false))  

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -