[R] bind tables

arun smartpink111 at yahoo.com
Sat Jan 12 19:45:43 CET 2013


Hi Matteo,
You could do this:
dat1<-read.table(text="
year  h  len fre
1994  5 10.5  2
1994  5 14.0  2
1994  5 11.5  1
1994  9 13.0  3
1994  9 11.5  1
1994  9 13.5  5
",sep="",header=TRUE)


res<-do.call(rbind,lapply(split(dat1,dat1$fre),function(x) x[rep(row.names(x),x$fre),1:3]))
row.names(res)<-1:nrow(res)
 res
#   year h  len
#1  1994 5 11.5
#2  1994 9 11.5
#3  1994 5 10.5
#4  1994 5 10.5
#5  1994 5 14.0
#6  1994 5 14.0
#7  1994 9 13.0
#8  1994 9 13.0
#9  1994 9 13.0
#10 1994 9 13.5
#11 1994 9 13.5
#12 1994 9 13.5
#13 1994 9 13.5
#14 1994 9 13.5
A.K.



----- Original Message -----
From: "mmurenu at tiscali.it" <mmurenu at tiscali.it>
To: smartpink111 at yahoo.com
Cc: 
Sent: Saturday, January 12, 2013 1:23 PM
Subject: bind tables

Hi Arun,
Thank you very much for your reply.
I know that I was not clear enough, since my basic knowledge of r language.
To be more clear....

I have this df:

year  h  len fre
1994  5 10.5   2
1994  5 14.0   2
1994  5 11.5   1
1994  9 13.0   3
1994  9 11.5   1
1994  9 13.5   5


MY goal is to obtain:

year  h  len
1994  5 10.5
1994  5 10.5
1994  5 14.0
1994  5 14.0
1994  5 11.5
1994  9 13.0
1994  9 13.0
1994  9 13.0
1994  9 11.5
1994  9 13.5
1994  9 13.5
1994  9 13.5

i.e. disaggregate the freq variable.

To do that I'm trying to rbind as follow:
each row with fre=2 twice (aa,aa) having subset aa for fre=2
each row with fre=3 three times (aa,aa,aa) having subset aa for fre=3
each row with fre=3 three times (aa,aa,aa,aa,aa) having subset aa for fre=5

You probably know a faster and less rude way :)
best
Matteo




More information about the R-help mailing list