sorting - R: writing the data related to each unique occurence into a separate file using for loop -


i beginner coding in r. have 60 unique id's in column each unique id having 30 entries, write code automatically creates separate files each unique id. code worked single id

unique(src$id) id2<- subset(src, id=='099857') write.csv(pat2,file= "d:/r/id2.csv") 

when try loop using following code.

for (i in 1:length(unique(src$id))) { unique(src$id)   id<- subset(src, id== "i")   paste(id)   write.csv(i,file="d:r/i.csv") } 

i file counts unique ids (60) , pastes them excel sheet.

trying incorporate structure single id automated loop.

expected output- 60 unique files 30 entries each.

does have suggestions? thank you.

you iterating on numeric vector (1:length(unique(src$id)), in loop refer character vector "i". try changing to:

for(i in unique(src$id)) { id2 <- subset(src, id == i) write.csv(id2, file = paste0("d:/r/",i,".csv")) } 

...if name of column contains id "sampleid," should change to:

 for(i in unique(src$sampleid)) {     id2 <- subset(src, sampleid == i)     write.csv(id2, file = paste0("d:/r/",i,".csv"))     } 

Comments