如何将 tapply() 的输出转换为 data.frame [英] How can I make the output from tapply() into a data.frame

查看:42
本文介绍了如何将 tapply() 的输出转换为 data.frame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 R 中有一个 data.frame,如下所示:

I have a data.frame in R that looks like this:

      score    rms  template   aln_id       description
1  -261.410  4.951 2f22A.pdb  2F22A_1 S_00001_0000002_0
2  -231.987 21.813 1wb9A.pdb  1WB9A_4 S_00002_0000002_0
3  -263.722  4.903 2f22A.pdb  2F22A_3 S_00003_0000002_0
4  -269.681 17.732 1wbbA.pdb  1WBBA_6 S_00004_0000002_0
5  -258.621 19.098 1rxqA.pdb  1RXQA_3 S_00005_0000002_0
6  -246.805  6.889 1rxqA.pdb 1RXQA_15 S_00006_0000002_0
7  -281.300 16.262 1wbdA.pdb 1WBDA_11 S_00007_0000002_0
8  -271.666  4.193 2f22A.pdb  2F22A_2 S_00008_0000002_0
9  -277.964 13.066 1wb9A.pdb  1WB9A_5 S_00009_0000002_0
10 -261.024 17.153 1yy9A.pdb  1YY9A_2 S_00001_0000003_0

我可以像这样计算 data.frame 上的汇总统计:

I can calculate summary statistics on the data.frame like this:

> tapply( d$score, d$template, mean )
1rxqA.pdb 1wb9A.pdb 1wbbA.pdb 1wbdA.pdb 1yy9A.pdb 2f22A.pdb 
-252.7130 -254.9755 -269.6810 -281.3000 -261.0240 -265.5993 

是否有一种简单的方法可以将此输出强制返回到 data.frame 中?我希望它有这两列:

Is there an easy way that I coerce this output back into a data.frame? I'd like for it to have these two columns:

d$template
mean

我喜欢 tapply,但现在我正在将 tapply 的结果剪切并粘贴到一个文本文件中,并对其进行一些修改以获得我想要的带有适当名称的汇总统计信息.这感觉很不对,我想做得更好!

I love tapply, but right now I'm cutting and pasting the results from tapply into a text file and hacking it up a bit to get the summary statistics that I want with appropriate names. This feels very wrong, and I'd like to do something better!

推荐答案

library(plyr)
ddply(d, "template", summarise, mean = mean(score))

这篇关于如何将 tapply() 的输出转换为 data.frame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆