[R] Extract values from vector and repeat by group

Berend Hasselman bhh at xs4all.nl
Sun Nov 17 16:20:27 CET 2013


On 17-11-2013, at 15:47, Benjamin Gillespie <gybrg at Leeds.ac.uk> wrote:

> Hi all,
> 
> I hope you can help.
> 
> I have a data frame 'df':
> 
> group=c(rep(1,8),rep(2,10),rep(3,11))
> var=rnorm(29)
> time=c(seq(1,8),seq(1,10),seq(1,11))
> df=data.frame(group,var,time)
> 
> I would like to extract the value from 'var' for each 'group' at 'time'=4 and repeat these extracted values in a new vector ('new') n times where n is the number of rows for each group. I did this by hand as below, but there must be a quicker way:
> 
> subset=subset(df,df$time==4)
> subset
> group        var time
> 4      1  0.2531270    4
> 12     2 -0.3600128    4
> 22     3  0.4194730    4
> 
> df$new=c(rep(0.2531270,8),rep(-0.3600128,10),rep(0.4194730,11))
> 
> Any questions please ask,

A very similar question was recently asked on Stackoverflow: http://stackoverflow.com/questions/19971763/r-programming-normalizing-a-column-of-data-by-another-entry-in-2-other-columns

From the answer given there you could try this

set.seed(11) # to make it reproducible

group=c(rep(1,8),rep(2,10),rep(3,11))
var=rnorm(29)
time=c(seq(1,8),seq(1,10),seq(1,11))
df=data.frame(group,var,time)

#df
#df[df$time==4, c("group", "var")]
 
# merge into original data.frame
df <- merge(df, df[df$time == 4, c("group", "var")], by.x = "group", by.y = "group", suffixes = c("", "GroupSK0"))
df

Berend



More information about the R-help mailing list