[英]Generate new column in dataframe, counting duplicates by group
I want to generate a new variable in a dataset. 我想在数据集中生成一个新变量。 This variable should count the occurence of values in different groups, defined by another variable.
此变量应计算由另一个变量定义的不同组中的值的出现。
Here an example dataframe: 这是一个示例数据帧:
x <- c(1, 1, 2, 3, 3, 3, 4, 4)
y <- c(5, 4, 4, 5, 5, 5, 1, 1)
dat <- data.frame(x, y)
dat
x y
1 1 5
2 1 4
3 2 4
4 3 5
5 3 5
6 3 5
7 4 1
8 4 1
Now i want to generate a new variable, let's call it z. 现在我想生成一个新变量,我们称之为z。 z should count the occurence of duplicates in y by groups (groups defined by x: 1, 2, 3, 4).
z应该按组(由x定义的组:1,2,3,4)计算y中重复项的出现次数。 Therefore, the result should look like this:
因此,结果应如下所示:
x y z
1 1 5 1
2 1 4 1
3 2 4 1
4 3 5 1
5 3 5 2
6 3 5 3
7 4 1 1
8 4 1 2
Is there a way to do that with dplyr? 有没有办法用dplyr做到这一点?
An option is to do a group by and create a sequence column 一个选项是执行分组并创建序列列
library(dplyr)
dat %>%
group_by(x, y) %>%
mutate(z = row_number())
# A tibble: 8 x 3
# Groups: x, y [5]
# x y z
# <dbl> <dbl> <int>
#1 1 5 1
#2 1 4 1
#3 2 4 1
#4 3 5 1
#5 3 5 2
#6 3 5 3
#7 4 1 1
#8 4 1 2
Also with base R
还有
base R
dat$z <- with(dat, ave(seq_along(x), x, y, FUN = seq_along))
Or with data.table
或者使用
data.table
library(data.table)
setDT(dat)[, z := seq_len(.N), .(x, y)]
Or more compactly 或者更紧凑
setDT(dat)[, z := rowid(x, y)]
One possibility could be: 一种可能性是:
dat %>%
group_by(x) %>%
mutate(z = cumsum(duplicated(y)) + 1)
x y z
<dbl> <dbl> <dbl>
1 1 5 1
2 1 4 1
3 2 4 1
4 3 5 1
5 3 5 2
6 3 5 3
7 4 1 1
8 4 1 2
The same with base R
: 与
base R
相同:
with(dat, ave(y, x, FUN = function(x) cumsum(duplicated(x)) + 1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.