CodeGuy
CodeGuy

Reputation: 28905

Convert tapply output to data frame in 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?

Upvotes: 5

Views: 12494

Answers (2)

John
John

Reputation: 23758

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 )

Upvotes: 14

Hong Ooi
Hong Ooi

Reputation: 57686

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.

Upvotes: 11

Related Questions