Re: [R] Extract values from vector and repeat by group
Brilliant - thanks for all the really useful suggestions, problem = solved. Many thanks, Ben Gillespie, Research Postgraduate o---o School of Geography, University of Leeds, Leeds, LS2 9JT o---o Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 o---o http://www.geog.leeds.ac.uk/ o-o @RiversBenG o--o From: arun [smartpink...@yahoo.com] Sent: 17 November 2013 16:48 To: R help Cc: Berend Hasselman; Benjamin Gillespie Subject: Re: [R] Extract values from vector and repeat by group Hi, ?merge() sometimes change the order. For example: df1 - df[-12,] df2 - df1 merge(df1, df1[df1$time == 4, c(group, var)], by.x = group, by.y = group, suffixes = c(, GroupSK0)) In that case, df1$ord1 - with(df1,order(group,time)) res - merge(df1, df1[df1$time == 4, c(group, var)], by.x = group, by.y = group, suffixes = c(, GroupSK0)) res[order(res$ord1),-4] #or just library(plyr) join(df2,df2[df2$time==4,c(group,var)],by=group,type=inner) #or you may use: indx - with(df1,ave(time==4,group,FUN=any)) ddply(df1[indx,],.(group),mutate,new=var[time==4]) A.K. On Sunday, November 17, 2013 10:22 AM, Berend Hasselman b...@xs4all.nl wrote: On 17-11-2013, at 15:47, Benjamin Gillespie gy...@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 groupvar time 4 1 0.25312704 12 2 -0.36001284 22 3 0.41947304 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 __ 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. __ 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.
Re: [R] Extract values from vector and repeat by group
Hi probably not most elegant and also not general but rep(df$var[df$time==4],rle(df$group)$lengths) or rep(df$var[df$time==4], sapply(split(df$var,df$group), length)) shall give you desired vector. Regards Petr -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of Benjamin Gillespie Sent: Sunday, November 17, 2013 3:47 PM To: r-help@r-project.org Subject: [R] Extract values from vector and repeat by group 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 groupvar time 4 1 0.25312704 12 2 -0.36001284 22 3 0.41947304 df$new=c(rep(0.2531270,8),rep(-0.3600128,10),rep(0.4194730,11)) Any questions please ask, Many thanks in advance, Ben Gillespie, Research Postgraduate o---o School of Geography, University of Leeds, Leeds, LS2 9JT o- --o Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 o---o http://www.geog.leeds.ac.uk/ o-o @RiversBenG o--o __ 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. __ 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.
Re: [R] Extract values from vector and repeat by group
Hi, I would also add an index to make it work for groups that doesn't have time=4. df1 - df[-12,] fun1 - function(dat,n) { indx - with(dat,tapply(time==n,group,FUN=any)) indx2 - with(dat,ave(time==n,group,FUN=any)) dat[indx2,new] - rep(dat$var[dat$time==n],rle(dat$group)$lengths[indx]) dat[!is.na(dat$new),] } fun1(df,4) fun1(df1,4) A.K. On Monday, November 18, 2013 9:18 AM, PIKAL Petr petr.pi...@precheza.cz wrote: Hi probably not most elegant and also not general but rep(df$var[df$time==4],rle(df$group)$lengths) or rep(df$var[df$time==4], sapply(split(df$var,df$group), length)) shall give you desired vector. Regards Petr -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of Benjamin Gillespie Sent: Sunday, November 17, 2013 3:47 PM To: r-help@r-project.org Subject: [R] Extract values from vector and repeat by group 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, Many thanks in advance, Ben Gillespie, Research Postgraduate o---o School of Geography, University of Leeds, Leeds, LS2 9JT o- --o Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 o---o http://www.geog.leeds.ac.uk/ o-o @RiversBenG o--o __ 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. __ 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. __ 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.
[R] Extract values from vector and repeat by group
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 groupvar time 4 1 0.25312704 12 2 -0.36001284 22 3 0.41947304 df$new=c(rep(0.2531270,8),rep(-0.3600128,10),rep(0.4194730,11)) Any questions please ask, Many thanks in advance, Ben Gillespie, Research Postgraduate o---o School of Geography, University of Leeds, Leeds, LS2 9JT o---o Tel: +44(0)113 34 33345 Mob: +44(0)770 868 7641 o---o http://www.geog.leeds.ac.uk/ o-o @RiversBenG o--o __ 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.
Re: [R] Extract values from vector and repeat by group
On 17-11-2013, at 15:47, Benjamin Gillespie gy...@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 groupvar time 4 1 0.25312704 12 2 -0.36001284 22 3 0.41947304 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 __ 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.
Re: [R] Extract values from vector and repeat by group
Hi, ?merge() sometimes change the order. For example: df1 - df[-12,] df2 - df1 merge(df1, df1[df1$time == 4, c(group, var)], by.x = group, by.y = group, suffixes = c(, GroupSK0)) In that case, df1$ord1 - with(df1,order(group,time)) res - merge(df1, df1[df1$time == 4, c(group, var)], by.x = group, by.y = group, suffixes = c(, GroupSK0)) res[order(res$ord1),-4] #or just library(plyr) join(df2,df2[df2$time==4,c(group,var)],by=group,type=inner) #or you may use: indx - with(df1,ave(time==4,group,FUN=any)) ddply(df1[indx,],.(group),mutate,new=var[time==4]) A.K. On Sunday, November 17, 2013 10:22 AM, Berend Hasselman b...@xs4all.nl wrote: On 17-11-2013, at 15:47, Benjamin Gillespie gy...@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 __ 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. __ 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.