4

我想使用 R 在我的数据集中创建一个新列,其中包含每个唯一组的最大值。我的数据如下所示:

group<-c("A","A","A","A","A","B","B","C","C","C")
replicate<-c(1,2,3,4,5,1,2,1,2,3)
x<-data.frame(cbind(group,replicate))

我想创建如下所示的第三列 - 每个组的最大值。

group   replicate max.per.group
A       1         5       
A       2         5
A       3         5
A       4         5
A       5         5
B       1         2
B       2         2
C       1         3
C       2         3
C       3         3
4

7 回答 7

4

如果您x首先重新定义(cbind使两列因素),

x<-data.frame(group,replicate)

你可以使用这个:

merge(x,aggregate(replicate~group,x,FUN=max),all.x=TRUE,by="group")
   group replicate.x replicate.y
1      A           1           5
2      A           2           5
3      A           3           5
4      A           4           5
5      A           5           5
6      B           1           2
7      B           2           2
8      C           1           3
9      C           2           3
10     C           3           3
于 2012-07-26T18:28:19.817 回答
3

尝试

# This is how you create your data.frame
group<-c("A","A","A","A","A","B","B","C","C","C")
replicate<-c(1,2,3,4,5,1,2,1,2,3)
x<-data.frame(group,replicate) # here you don't need c()

# Here's my solution    
Max <- tapply(x$replicate, x$group,max)
data.frame(x, max.per.group=rep(Max, table(x$group)))
 group replicate max.per.group
1      A         1             5
2      A         2             5
3      A         3             5
4      A         4             5
5      A         5             5
6      B         1             2
7      B         2             2
8      C         1             3
9      C         2             3
10     C         3             3
于 2012-07-26T18:24:42.050 回答
3

这是另一个基本 R 解决方案:

cbind(x, cummax=unlist(tapply(x$replicate, x$group, function(x) rep(max(x), length(x)))))
   group replicate cummax
A1     A         1      5
A2     A         2      5
A3     A         3      5
A4     A         4      5
A5     A         5      5
B1     B         1      2
B2     B         2      2
C1     C         1      3
C2     C         2      3
C3     C         3      3
于 2012-07-26T18:26:08.267 回答
3

共享的可重现示例建议您将列作为因子。我们需要先将它们转换为数字。

我们可以尝试使用 base R ave。找出每组中的最大值。

x$max.per.group <- ave(x$replicate, x$group, FUN = function(x) max(as.numeric(x)))

#   group replicate max.per.group
#1      A         1             5
#2      A         2             5
#3      A         3             5
#4      A         4             5
#5      A         5             5
#6      B         1             2
#7      B         2             2
#8      C         1             3
#9      C         2             3
#10     C         3             3

另一种选择dplyr

library(dplyr)
x %>%
   group_by(group) %>%
   mutate(max.per.group = max(as.numeric(replicate)))
于 2017-02-15T05:21:19.163 回答
2

你可以使用这个plyr包:

library(plyr)
> ddply(x, .(group), transform, max.per.group=max(replicate))
   group replicate max.per.group
1      A         1             5
2      A         2             5
3      A         3             5
4      A         4             5
5      A         5             5
6      B         1             2
7      B         2             2
8      C         1             3
9      C         2             3
10     C         3             3
> 
于 2012-07-26T18:22:09.127 回答
1

你可以使用rle-Run Length Encoding

# Create the data.frame
group <- c("A","A","A","A","A","B","B","C","C","C")
replicate <- c(1,2,3,4,5,1,2,1,2,3)
x <- data.frame(group,replicate)

# using 'rle'
z <- rle(as.numeric(x$group))$lengths
x$max.per.group <- rep(z, z)
x
于 2012-07-26T21:04:09.263 回答
0

我们可以使用data.table赋值(:=)在原地创建一列,无需复制

library(data.table)
setDT(x)[, max.per.group := max(replicate), by = group]
x
#     group replicate max.per.group
#1:     A         1             5
#2:     A         2             5
#3:     A         3             5
#4:     A         4             5
#5:     A         5             5
#6:     B         1             2
#7:     B         2             2
#8:     C         1             3
#9:     C         2             3
#10:    C         3             3

数据

x <- data.frame(group,replicate)
于 2017-02-15T05:47:16.433 回答