ozan bakis
2009-Apr-23 22:11 UTC
[R] conditional grouping of variables: ave or tapply or by or???
Dear R Users, I have the following data frame: v1 <- c(rep(10,3),rep(11,2)) v2 <- sample(5:10, 5, replace = T) v3 <- c(0,1,2,0,2) df <- data.frame(v1,v2,v3)> dfv1 v2 v3 1 10 9 0 2 10 5 1 3 10 6 2 4 11 7 0 5 11 5 2 I want to add a new column v4 such that its values are equal to the value of v2 conditional on v3=0 for each subgroup of v1. In the above example, the final result should be like df$v4 <- c(9,9,9,7,7)> dfv1 v2 v3 v4 1 10 9 0 9 2 10 5 1 9 3 10 6 2 9 4 11 7 0 7 5 11 5 2 7 I tried the following commands without success. df$v4 <- ave(df$v2, df$v1, FUN=function(x) x[df$v3==0]) tapply(df$v2, df$v1, FUN=function(x) x[df$v3==0]) by(df$v2, df$v1, FUN=function(x) x[df$v3==0]) Any help? Thanks in advance! Ozan [[alternative HTML version deleted]]
hadley wickham
2009-Apr-23 23:50 UTC
[R] conditional grouping of variables: ave or tapply or by or???
On Thu, Apr 23, 2009 at 5:11 PM, ozan bakis <ozanbakis at gmail.com> wrote:> Dear R Users, > I have the following data frame: > > v1 <- c(rep(10,3),rep(11,2)) > v2 <- sample(5:10, 5, replace = T) > v3 <- c(0,1,2,0,2) > df <- data.frame(v1,v2,v3) >> df > ?v1 v2 v3 > 1 10 ?9 ?0 > 2 10 ?5 ?1 > 3 10 ?6 ?2 > 4 11 ?7 ?0 > 5 11 ?5 ?2 > > I want to add a new column v4 such that its values are equal to the value > of v2 conditional on v3=0 for each subgroup of v1. In the above example, > the final result should be like > > df$v4 <- c(9,9,9,7,7) >> df > ?v1 v2 v3 v4 > 1 10 ?9 ?0 ?9 > 2 10 ?5 ?1 ?9 > 3 10 ?6 ?2 ?9 > 4 11 ?7 ?0 ?7 > 5 11 ?5 ?2 ?7 > > > I tried the following commands without success. > > df$v4 <- ave(df$v2, df$v1, FUN=function(x) x[df$v3==0]) > tapply(df$v2, df$v1, FUN=function(x) x[df$v3==0]) > by(df$v2, df$v1, FUN=function(x) x[df$v3==0]) > > Any help? Thanks in advance!Here's one approach with the plyr package, http://had.co.nz/plyr library(plyr) ddply(df, .(v1), transform, v4 = v2[v3 == 0]) Hadley -- http://had.co.nz/
Gabor Grothendieck
2009-Apr-24 00:10 UTC
[R] conditional grouping of variables: ave or tapply or by or???
Try this:> df$v4 <- ave(1:nrow(df), df$v1, FUN = function(i) with(df[i,], v2[!v3])) > dfv1 v2 v3 v4 1 10 7 0 7 2 10 5 1 7 3 10 7 2 7 4 11 9 0 9 5 11 7 2 9> # If as is the case here that 0 is always the first in each v1 group > # then it can be simplified further to:> df$v4 <- with(df, ave(v2, v1, FUN = function(x) x[1]))On Thu, Apr 23, 2009 at 6:11 PM, ozan bakis <ozanbakis at gmail.com> wrote:> Dear R Users, > I have the following data frame: > > v1 <- c(rep(10,3),rep(11,2)) > v2 <- sample(5:10, 5, replace = T) > v3 <- c(0,1,2,0,2) > df <- data.frame(v1,v2,v3) >> df > ?v1 v2 v3 > 1 10 ?9 ?0 > 2 10 ?5 ?1 > 3 10 ?6 ?2 > 4 11 ?7 ?0 > 5 11 ?5 ?2 > > I want to add a new column v4 such that its values are equal to the value > of v2 conditional on v3=0 for each subgroup of v1. In the above example, > the final result should be like > > df$v4 <- c(9,9,9,7,7) >> df > ?v1 v2 v3 v4 > 1 10 ?9 ?0 ?9 > 2 10 ?5 ?1 ?9 > 3 10 ?6 ?2 ?9 > 4 11 ?7 ?0 ?7 > 5 11 ?5 ?2 ?7 > > > I tried the following commands without success. > > df$v4 <- ave(df$v2, df$v1, FUN=function(x) x[df$v3==0]) > tapply(df$v2, df$v1, FUN=function(x) x[df$v3==0]) > by(df$v2, df$v1, FUN=function(x) x[df$v3==0]) > > Any help? Thanks in advance! > Ozan > > ? ? ? ?[[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. >