[R] how to rbind matrices from different loops

jim holtman jholtman at gmail.com
Thu Feb 16 17:25:23 CET 2012


Is this close to what you want:

> 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 = paste(unique(.grp$id), collapse = ',')
+                 )
+         })
+ )
>
> uniqueIDs
   group                                   unique
1      1  175,186,226,318,458,609,633,682,719,869
2      2 290,361,408,487,500,559,773,779,809,1459
3      3 201,283,371,470,479,513,576,719,810,1037
4      4   4,18,118,212,291,482,738,818,1008,1095
5      5 115,243,253,385,396,447,547,625,790,1156
6      6  175,186,226,318,458,609,633,682,719,869
7      7 290,361,408,487,500,559,773,779,809,1459
8      8 201,283,371,470,479,513,576,719,810,1037
9      9   4,18,118,212,291,482,738,818,1008,1095
10    10 115,243,253,385,396,447,547,625,790,1156
11    11  175,186,226,318,458,609,633,682,719,869
12    12 290,361,408,487,500,559,773,779,809,1459
13    13 201,283,371,470,479,513,576,719,810,1037
14    14   4,18,118,212,291,482,738,818,1008,1095
15    15 115,243,253,385,396,447,547,625,790,1156
>

On Thu, Feb 16, 2012 at 11:15 AM, Matt Spitzer
<matthewjspitzer at 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 at 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.



More information about the R-help mailing list