Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert tapply output to data frame in R

Tags:

r

I have some output from a tapply call that looks like

1        2       4
678.2    19.3    716.2

and I want to make it into a data frame that looks like

     key    value
1    1      678.2
2    2      19.3
3    4      716.2

how can I do this?

like image 709
CodeGuy Avatar asked Nov 22 '11 05:11

CodeGuy


2 Answers

If you replace tapply with aggregate it will automatically come out as a data.frame.

aggregate( value ~ key, FUN = mean )

where value and key correspond to the tapply call

tapply( value, key, mean )
like image 183
John Avatar answered Nov 11 '22 08:11

John


Some example code would be nice, to see exactly what you're doing. To answer your question narrowly, if x is the result of your tapply, then

data.frame(key=names(x), value=x)

More broadly, see ?by and ?aggregate, and also package plyr for more general data-wrangling needs.

like image 11
Hong Ooi Avatar answered Nov 11 '22 08:11

Hong Ooi