如何通过通用ID从大型数据集中提取行,并采取这些行的方式,并使列具有这些ID [英] How can I extract the rows from a large data set by common IDs and take the means of these rows and make a column having these IDs

查看:97
本文介绍了如何通过通用ID从大型数据集中提取行,并采取这些行的方式,并使列具有这些ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个非常愚蠢的问题,但我不能整理出来,这就是为什么要求...
如何从一个大的数据集中的公共ID提取行,并采取这些行的方式并将具有这些ID的列作为rownames。
eg

I know it is a very silly question but I could not sort it out that is why asking... How can I extract the rows from a large data set by common IDs and take the means of these rows and make a column having these IDs as rownames. e.g.

IDs Var2    
Ae4 2       
Ae4 4
Ae4 6
Bc3 3   
Bc3 5   
Ad2 8
Ad2 7

OutPut
Var(x)
Ae4 4
Bc3 4
Ad2 7.5


推荐答案

事情可以很容易地使用 plyr 函数 ddply

This kinds of things can easily be done using the plyr function ddply:

dat = data.frame(ID = rep(LETTERS[1:5], each = 20), value = runif(100))
> head(dat)
  ID      value
1  A 0.45800889
2  A 0.11221072
3  A 0.58833532
4  A 0.70056704
5  A 0.08337996
6  A 0.05195357

ddply(dat, .(ID), summarize, mn = mean(value))
  ID        mn
1  A 0.4960083
2  B 0.5809681
3  C 0.4512388
4  D 0.5079790
5  E 0.5397708

数据集很大,和/或唯一 ID 的数量很大,可以使用 data.table 。有关 plyr 的详细信息,请参见本白皮书。 。

If your dataset is big, and/or the number of unique ID's is big, you could use data.table. See this paper for more detail about plyr.

这篇关于如何通过通用ID从大型数据集中提取行,并采取这些行的方式,并使列具有这些ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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