重复数字的序列

4

数据

我有一个类似下面这样的data.frame

df <- data.frame(id = c(1:10),
                 color = c(rep("red", 5), rep("blue", 5)))
df
#>    id color
#> 1   1   red
#> 2   2   red
#> 3   3   red
#> 4   4   red
#> 5   5   red
#> 6   6  blue
#> 7   7  blue
#> 8   8  blue
#> 9   9  blue
#> 10 10  blue

预期结果

我正在尝试创建一个新列,称为pair,将一对连续的ID分配给每个组。例如,我希望最终得到一个类似于data.frame的表格:

df
#>    id color pair
#> 1   1   red    1
#> 2   2   red    1
#> 3   3   red    2
#> 4   4   red    2
#> 5   5   red    3
#> 6   6  blue    3
#> 7   7  blue    4
#> 8   8  blue    4
#> 9   9  blue    5
#> 10 10  blue    5

当前方法

我只想知道是否有比我已经做的更简洁的方法来实现这个目标。我已经查看了seq()文档,但没有找到任何帮助。这是我目前的方法,可以得到我想要的输出,但不够简洁。

df %>% 
  dplyr::mutate(pair = sort(rep(seq(length.out = nrow(df)/2),2)))

#     id  color   pair
# 1   1   red    1
# 2   2   red    1
# 3   3   red    2
# 4   4   red    2
# 5   5   red    3
# 6   6  blue    3
# 7   7  blue    4
# 8   8  blue    4
# 9   9  blue    5
# 10 10  blue    5

有没有其他想法或函数可以完成这项工作,除了seq()

4个回答

3

以下是使用基本R中的rep()函数的简单示例 -

df$pair <- rep(1:nrow(df), each = 2, length.out = nrow(df))

df

   id color pair
1   1   red    1
2   2   red    1
3   3   red    2
4   4   red    2
5   5   red    3
6   6  blue    3
7   7  blue    4
8   8  blue    4
9   9  blue    5
10 10  blue    5

使用dplyr-

df %>% 
  mutate(pair = rep(1:nrow(.), each = 2, length.out = nrow(.)))

1
一种可能性是:

可以是:

df %>%
 mutate(pair = gl(n()/2, 2))

   id color pair
1   1   red    1
2   2   red    1
3   3   red    2
4   4   red    2
5   5   red    3
6   6  blue    3
7   7  blue    4
8   8  blue    4
9   9  blue    5
10 10  blue    5

1
我们可能会使用整数除法。
(df$pair <- (1:nrow(df) - 1) %/% 2)
#  [1] 0 0 1 1 2 2 3 3 4 4

这也很好地推广到更大的群体; 例如,

(df$pair <- (1:nrow(df) - 1) %/% 3)
#  [1] 0 0 0 1 1 1 2 2 2 3

1
另一个选项。
library(dplyr)
df %>%
   mutate(pair = as.integer(gl(n(), 2, n())))
#    id color pair
#1   1   red    1
#2   2   red    1
#3   3   red    2
#4   4   red    2
#5   5   red    3
#6   6  blue    3
#7   7  blue    4
#8   8  blue    4
#9   9  blue    5
#10 10  blue    5

或者使用repcumsum函数。
df %>% 
    mutate(pair = cumsum(rep(c(TRUE, FALSE), length.out = n())))

或者更简单的情况是使用基本R

df$pair <- c(TRUE, FALSE)
df$pair <- cumsum(df$pair)

1
非常感谢您提供的众多解决方案!我已经记录下来以备将来使用。 - Felix T.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接