Dear R experts, I am having difficulty using loops productively and would like to please ask for advice. I have a dataframe of ids and groups. I would like to break down the dataframe into groups, find the unique sets of ids, then reassemble. My thought was to use a loop, but I have been unable to finish this loop in a logical way. I would like to find the unique ids for group 1, group 2, etc., and rbind these back together. However, I am unclear how to do this because in other attempts my final product is always a part of the last run loop. My way of working around this has been to write.csv and then re read at the end, which is so clumsy. Previously, I have used a store matrix for individual cells. 1. Is there a better way to approach this? 2. How can I combine parts of matrices to other parts created in prior loops? I have created a primitive example below. Each of the groups varies in number, so my repetitive example below is not accurate. In my real data, ids repeat often within groups. Thank you so much, Matt
example <- data.frame(id=rep( ( abs(round(rnorm(50,mean=500,sd=250),digits=0))) ,3), group=rep(1:15,10)) example <-example[with(example,order(id,group)),] for (i in 1:15) { ai <- example[example[,2]==i,][!duplicated ( example[example[,2]==i,][,1] ),] write.csv(ai, paste('a',i,'.csv',sep="")) } b1<-read.csv('a1.csv') b2<-read.csv('a2.csv') b3<-read.csv('a3.csv') b4<-read.csv('a4.csv') b5<-read.csv('a5.csv') b6<-read.csv('a6.csv') b7<-read.csv('a7.csv') b8<-read.csv('a8.csv') b9<-read.csv('a9.csv') b10<-read.csv('a10.csv') b11<-read.csv('a11.csv') b12<-read.csv('a12.csv') b13<-read.csv('a13.csv') unis2 <- rbind(rbind(rbind(rbind(rbind(rbind(rbind(rbind(rbind(rbind(rbind(rbind (b1,b2),b3),b4),b5),b6),b7),b8),b9),b10),b11),b12),b13) [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.