什么';使用ddply/plyr对数据帧中每列重复行的值求平均值的通用方法是什么?
假设我有一个如下所示的数据帧:什么';使用ddply/plyr对数据帧中每列重复行的值求平均值的通用方法是什么?,r,plyr,R,Plyr,假设我有一个如下所示的数据帧: Name A1 A2 A3 B1 B2 B3 C1 C2 C3 100 3 6 2 9 2 2 1 5 1 200 5 3 7 3 2 6 3 8 3 200 5 5 9 5 0 4 5 4 1 300 4 8 2 7 0 2
Name A1 A2 A3 B1 B2 B3 C1 C2 C3
100 3 6 2 9 2 2 1 5 1
200 5 3 7 3 2 6 3 8 3
200 5 5 9 5 0 4 5 4 1
300 4 8 2 7 0 2 1 4 9
名为200
的项目重复。我想对这两行200
的每一列的值求平均值,因此我只在行中表示唯一的项:
Name A1 A2 A3 B1 B2 B3 C1 C2 C3
100 3 6 2 9 2 2 1 5 1
200 5 4 8 4 1 5 4 6 2
300 4 8 2 7 0 2 1 4 9
我的新手R编码技能引导我使用以下方法:
Name A1 A2 A3 B1 B2 B3 C1 C2 C3
100 3 6 2 9 2 2 1 5 1
200 5 3 7 3 2 6 3 8 3
200 5 5 9 5 0 4 5 4 1
300 4 8 2 7 0 2 1 4 9
已删除。重复项应该可以。我将您的示例放在一个名为df的data.frame中
library(dplyr)
df %>% group_by(Name) %>% summarise_each(funs(mean))
这应该可以做到。我将您的示例放在一个名为df的data.frame中
library(dplyr)
df %>% group_by(Name) %>% summarise_each(funs(mean))
在更复杂的数据上使用base R:
> ddf
Name A1 A2 A3 B1 B2 B3 C1 C2 C3
1 100 3 6 2 9 2 2 1 5 1
2 200 5 3 7 3 2 6 3 8 3
3 200 5 5 9 5 0 4 5 4 1
4 300 4 8 2 7 0 2 1 4 9
5 300 2 4 2 9 6 6 5 8 1
6 300 3 6 2 5 8 8 6 6 5
7 500 4 8 2 7 0 2 1 4 9
>
> ddf = structure(list(Name = c(100L, 200L, 200L, 300L, 300L, 300L, 500L
+ ), A1 = c(3L, 5L, 5L, 4L, 2L, 3L, 4L), A2 = c(6L, 3L, 5L, 8L,
+ 4L, 6L, 8L), A3 = c(2L, 7L, 9L, 2L, 2L, 2L, 2L), B1 = c(9L, 3L,
+ 5L, 7L, 9L, 5L, 7L), B2 = c(2L, 2L, 0L, 0L, 6L, 8L, 0L), B3 = c(2L,
+ 6L, 4L, 2L, 6L, 8L, 2L), C1 = c(1L, 3L, 5L, 1L, 5L, 6L, 1L),
+ C2 = c(5L, 8L, 4L, 4L, 8L, 6L, 4L), C3 = c(1L, 3L, 1L, 9L,
+ 1L, 5L, 9L)), .Names = c("Name", "A1", "A2", "A3", "B1",
+ "B2", "B3", "C1", "C2", "C3"), class = "data.frame", row.names = c(NA,
+ -7L))
>
> dups = ddf[duplicated(ddf$Name),]$Name
> ddf2 = ddf[!ddf$Name %in% dups,]
> for(nn in unique(dups)) ddf2 = rbind(ddf2, colMeans(ddf[ddf$Name==nn,]))
> ddf2
Name A1 A2 A3 B1 B2 B3 C1 C2 C3
1 100 3 6 2 9 2.000000 2.000000 1 5 1
7 500 4 8 2 7 0.000000 2.000000 1 4 9
3 200 5 4 8 4 1.000000 5.000000 4 6 2
4 300 3 6 2 7 4.666667 5.333333 4 6 5
在更复杂的数据上使用base R:
> ddf
Name A1 A2 A3 B1 B2 B3 C1 C2 C3
1 100 3 6 2 9 2 2 1 5 1
2 200 5 3 7 3 2 6 3 8 3
3 200 5 5 9 5 0 4 5 4 1
4 300 4 8 2 7 0 2 1 4 9
5 300 2 4 2 9 6 6 5 8 1
6 300 3 6 2 5 8 8 6 6 5
7 500 4 8 2 7 0 2 1 4 9
>
> ddf = structure(list(Name = c(100L, 200L, 200L, 300L, 300L, 300L, 500L
+ ), A1 = c(3L, 5L, 5L, 4L, 2L, 3L, 4L), A2 = c(6L, 3L, 5L, 8L,
+ 4L, 6L, 8L), A3 = c(2L, 7L, 9L, 2L, 2L, 2L, 2L), B1 = c(9L, 3L,
+ 5L, 7L, 9L, 5L, 7L), B2 = c(2L, 2L, 0L, 0L, 6L, 8L, 0L), B3 = c(2L,
+ 6L, 4L, 2L, 6L, 8L, 2L), C1 = c(1L, 3L, 5L, 1L, 5L, 6L, 1L),
+ C2 = c(5L, 8L, 4L, 4L, 8L, 6L, 4L), C3 = c(1L, 3L, 1L, 9L,
+ 1L, 5L, 9L)), .Names = c("Name", "A1", "A2", "A3", "B1",
+ "B2", "B3", "C1", "C2", "C3"), class = "data.frame", row.names = c(NA,
+ -7L))
>
> dups = ddf[duplicated(ddf$Name),]$Name
> ddf2 = ddf[!ddf$Name %in% dups,]
> for(nn in unique(dups)) ddf2 = rbind(ddf2, colMeans(ddf[ddf$Name==nn,]))
> ddf2
Name A1 A2 A3 B1 B2 B3 C1 C2 C3
1 100 3 6 2 9 2.000000 2.000000 1 5 1
7 500 4 8 2 7 0.000000 2.000000 1 4 9
3 200 5 4 8 4 1.000000 5.000000 4 6 2
4 300 3 6 2 7 4.666667 5.333333 4 6 5
为了理解%
在R中是如何工作的,我进行了一次相当模糊的搜索,发现了基本函数aggregate()
,它的工作非常简单
删除。重复在一次相当模糊的搜索中,为了理解%
如何在R中工作,我遇到了基本函数aggregate()
,它的工作非常简单
删除。重复我有点困惑。。。%%>%%
是什么意思?我有点困惑。。。%%>%%
是什么意思?嗨。我应该提到%运算符用于创建中缀函数,如上面使用dplyr的示例中的%>%。%%>%运算符是在magrittr包中引入的,可以在多种设置中用于将操作串在一起。这里有一个专门指向magrittr Vignite页面的链接,其中有许多在dplyr之外使用的示例…——谢谢。我应该提到%运算符用于创建中缀函数,如上面使用dplyr的示例中的%>%。%%>%运算符是在magrittr包中引入的,可以在多种设置中用于将操作串在一起。这里有一个专门指向magrittr Vignite页面的链接,其中有许多在dplyr之外使用的示例…——干杯