Re: [R] add a new column to data frame

2010-10-01 Thread Henrique Dallazuanna
ing that can do this, I guess not possible. > > Thank you > > John > > > > > > From: Phil Spector > > Cc: r-help@r-project.org > Sent: Fri, October 1, 2010 10:58:55 AM > Subject: Re: [R] add a new column to data frame > > Here are two ways:

Re: [R] add a new column to data frame

2010-10-01 Thread array chip
is, I guess not possible. Thank you John From: Phil Spector Cc: r-help@r-project.org Sent: Fri, October 1, 2010 10:58:55 AM Subject: Re: [R] add a new column to data frame Here are two ways: dat0 = subset(dat,time==0) one = as.data.frame(as.table(

Re: [R] add a new column to data frame

2010-10-01 Thread Phil Spector
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(

[R] add a new column to data frame

2010-10-01 Thread array chip
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 right0 p1 3 2 left0 p1