[R] question about subseting a dataframe

Charles Plessy charles-listes-r at plessy.org
Sat May 10 05:36:47 CEST 2008


Le Fri, May 09, 2008 at 11:23:37PM -0400, Dipankar Basu a écrit :
> > ex <- data.frame(id=id,year=year,x=x)
> > ex1 <- subset(ex[which(ex$id=="A"|ex$id=="B"),])
> > tapply(ex1$x, ex1$id, mean)
>   A    B    C
> 22.5 32.5   NA

Dear Dipankar,

The reason for this behaviour is that the class of ex$id is "factor".
You can avoid this by using the I command, like in:

ex <- data.frame(id=I(id),year=year,x=x)

Have a nice day,

-- 
Charles Plessy
http://charles.plessy.org
Wakō, Saitama, Japan



More information about the R-help mailing list