If you want a row for each unique item:

> 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)),]
>
> uniqueIDs <- do.call(rbind
+     , lapply(split(example, example$group), function(.grp){
+         data.frame(group = .grp$group[1L]
+                 , unique = unique(.grp$id)
+                 )
+         })
+ )
>
> head(uniqueIDs, 25)
     group unique
1.1      1     15
1.2      1    282
1.3      1    361
1.4      1    497
1.5      1    553
1.6      1    583
1.7      1    618
1.8      1    729
1.9      1    817
1.10     1    842
2.1      2    135
2.2      2    253
2.3      2    290
2.4      2    351
2.5      2    418
2.6      2    457
2.7      2    518
2.8      2    594
2.9      2    686
2.10     2    804
3.1      3    208
3.2      3    327
3.3      3    442
3.4      3    516
3.5      3    536

On Thu, Feb 16, 2012 at 11:15 AM, Matt Spitzer
<matthewjspit...@gmail.com> wrote:
> 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.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

______________________________________________
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.

Reply via email to