[R] Odp: Tapply.

Petr PIKAL petr.pikal at precheza.cz
Mon Apr 26 09:26:37 CEST 2010


Hi

r-help-bounces at r-project.org napsal dne 26.04.2010 06:52:55:

> Having some difficulties with understanding how tapply works and getting
> return values I expect
> 
> Data: dataframe. DF  DF$Id $D $Year.......
> 
>  Id                          D  Year Jan Feb Mar Apr May Jun Jul Aug Sep 
Oct
> Nov Dec
>  11264402000         1 1980  NA  NA  NA  NA  NA 212 203 209 228 237  NA 
NA
>  11264402000         0 1981  NA  NA 243 244  NA  NA  NA  NA 225  NA 231 
NA
>  11264402000         1 1981  NA 251  NA 248 241  NA  NA  NA 235  NA  NA 
245
>  11264402000         0 1982 236 237 242 240 242 205 199  NA  NA  NA  NA 
NA
>  11264402000         1 1982 236  NA  NA 240 242  NA  NA  NA  NA  NA  NA 
NA
>  11264402000         0 1983  NA 247  NA  NA  NA  NA  NA 205  NA  NA  NA 
NA
>  11264402000         1 1983  NA 247  NA  NA  NA  NA  NA  NA  NA 225  NA 
NA
>  11264402000         0 1986  NA  NA  NA 240  NA  NA  NA 213  NA  NA  NA 
NA
>  11264402000         0 1987 241  NA  NA  NA  NA 218  NA  NA 235 243 240 
NA
>  11264402000         1 1987  NA  NA  NA  NA  NA 218  NA  NA 235 243 240 
NA
>  11264402000         3 1987  NA  NA  NA  NA  NA 218  NA  NA 235 243 240 
NA
>  11264402000         0 1988 238 246 249  NA 244 213 212 224 232 238 232 
230
>  11264402000         1 1988 238 246 249 246 244 213 212 224 232  NA  NA 
230
>  11264402000         3 1988 238 246 249 246 244 213 212 224 232  NA  NA 
230
>  11264402000         0 1989 232 233 238 239 231  NA 215  NA  NA  NA  NA 
238
>  11264402000         1 1989 232 233 238 239 231  NA  NA  NA  NA  NA  NA 
238
>  11264402000         3 1989 232 233 238 239 231  NA  NA  NA  NA  NA  NA 
238
> 
> and the result should be a dataframe of column means by year  with the
> variable D dropped (or kept doesnt matter)
> 
> 11264402000         1  1980  NA  NA  NA  NA  NA 212 203 209 228 237  NA 
NA
>  11264402000        .5  1981  NA  NA 243 244  NA  NA  NA  NA 225  NA 231 
 NA
>  11264402000        .5  1982 236 237 242 240 242 205 199  NA  NA  NA  NA 
 NA
>  11264402000        .5  1983  NA 247  NA  NA  NA  NA  NA 205  NA  225 NA
>  NA
>  11264402000        1  1986  NA  NA  NA 240  NA  NA  NA 213  NA  NA  NA 
NA
>  11264402000         2 1987 241  NA  NA  NA  NA 218  NA  NA 235 243 240 
NA
>  11264402000        1.33 1988 238 246 249  246 244 213 212 224 232 238 
232
> 230
>  11264402000        1.33  1989 232 233 238 239 231  NA 215  NA  NA  NA 
NA
> 238
> 
>  It would seem that Tapply should work
>  result<-tapply( DF[,1:15], DF$Year, colMeans,na.rm=T)

Why colMeans?  It is function used instead of apply(...,.. ,mean).

Maybe you want

result<-tapply( DF[,1:15], DF$Year, mean,na.rm=T)

Regards
Petr

> 
>  but i get errors about the length of arguments, which
> 
>    [[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