[R] add a new column to data frame
Phil Spector
spector at stat.berkeley.edu
Fri Oct 1 19:58:55 CEST 2010
Here are two ways:
dat0 = subset(dat,time==0)
one = as.data.frame(as.table(by(dat0,dat0$id,
function(x)as.character(x$mode)[which.max(x$y)])))
names(one) = c('id','type')
merge(dat,one)
two = sapply(split(dat0,dat0$id),
function(x)as.character(x$mode)[which.max(x$y)])
merge(dat,data.frame(id=names(two),type=two))
- Phil Spector
Statistical Computing Facility
Department of Statistics
UC Berkeley
spector at stat.berkeley.edu
On Fri, 1 Oct 2010, array chip wrote:
> Hi, I am wondering if anyone can propose a simple/best way to do the following:
>
> Let's say I have a data frame
>
> dat <-
> cbind(expand.grid(mode=c('right','left'),time=0:3,id=c('p1','p2','p3')),y=c(3,5,rep(4,6),6,2,rep(3,6),4,4,rep(2,6)))
>
>
> dat
> mode time id y
> 1 right 0 p1 3
> 2 left 0 p1 5
> 3 right 1 p1 4
> 4 left 1 p1 4
> 5 right 2 p1 4
> 6 left 2 p1 4
> 7 right 0 p2 6
> 8 left 0 p2 2
> 9 right 1 p2 3
> 10 left 1 p2 3
> 11 right 2 p2 3
> 12 left 2 p2 3
> 13 right 0 p3 4
> 14 left 0 p3 4
> 15 right 1 p3 2
> 16 left 1 p3 2
> 17 right 2 p3 2
> 18 left 2 p3 2
>
> Now I want to add a new column "type" to this data frame with values of either
> "left" or "right" for each "id" based on the following logic:
>
> For each "id", the value of "type" column is the value of "mode" for which the
> value of "y" is the maximum of "y" based on time=0. For example for id=p1, the
> value of "type" is "left" because for the 2 "y" values (3 & 5) based on time=0,
> mode=left has the bigger "y" value (5). But if the 2 "y" values are the same for
> "mode", then let type=right.
>
> In the end the new data frame is:
>
> mode time id y type
> 1 right 0 p1 3 left
> 2 left 0 p1 5 left
> 3 right 1 p1 4 left
> 4 left 1 p1 4 left
> 5 right 2 p1 4 left
> 6 left 2 p1 4 left
> 7 right 0 p2 6 right
> 8 left 0 p2 2 right
> 9 right 1 p2 3 right
> 10 left 1 p2 3 right
> 11 right 2 p2 3 right
> 12 left 2 p2 3 right
> 13 right 0 p3 4 right
> 14 left 0 p3 4 right
> 15 right 1 p3 2 right
> 16 left 1 p3 2 right
> 17 right 2 p3 2 right
> 18 left 2 p3 2 right
>
> Any suggestions would be appreciated.
>
> John
>
>
>
> [[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.
>
More information about the R-help
mailing list