我一直在寻找一段时间,但我无法找到解决方案。我有一个数据框,其中包含ID和VAR。我在下面试图复制一个样本
require(dplyr)
seed(123)
N <- 3
T <- 4
id <- rep(letters[1:N], each = T)
var <- rep(sample(seq(1:100),T),N)
row <- sample(seq(1:(N*T)),replace = F)
dt <- data.frame(ID=id,VAR=var,ROW=row) %>%
arrange(ROW) %>%
select(-ROW)
我想通过ID和VAR arrange
并为每个组添加一个计数器以获得类似
ID VAR COUNTER
1 a 1 1
2 a 11 2
3 a 22 3
4 a 64 4
5 b 1 1
6 b 11 2
7 b 22 3
8 b 64 4
9 c 1 1
10 c 11 2
11 c 22 3
12 c 64 4
所有这些,如果可能的话,只需使用dplyr或基本函数。
答案 0 :(得分:5)
在dplyr
内,您需要arrange()
ID
和VAR
以及group_by()
ID
{/ 1}}。{/ p>
然后使用mutate()
添加新列,从1到n()
计数(其中n()
是'行数'的dplyr函数)
set.seed(123)
dt %>%
arrange(ID, VAR) %>%
group_by(ID) %>%
mutate(COUNTER = 1:n()) %>% ## as per comment, can use row_number()
ungroup()
# # A tibble: 12 × 3
# ID VAR COUNTER
# <fctr> <int> <int>
# 1 a 29 1
# 2 a 41 2
# 3 a 79 3
# 4 a 86 4
# 5 b 29 1
# 6 b 41 2
# 7 b 79 3
# 8 b 86 4
# 9 c 29 1
# 10 c 41 2
# 11 c 79 3
# 12 c 86 4
我这样做是为了删除与grouped_df
相关联的所有“分组”属性。在这个例子中,结果是相同的,但那些分组的属性可能会让你更进一步。
dt_grouped <- dt %>%
arrange(ID, VAR) %>%
group_by(ID) %>%
mutate(COUNTER = 1:n())
dt_ungrouped <- dt %>%
arrange(ID, VAR) %>%
group_by(ID) %>%
mutate(COUNTER = 1:n()) %>%
ungroup()
str(dt_grouped)
# Classes ‘grouped_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 12 obs. of 3 variables:
# $ ID : Factor w/ 3 levels "a","b","c": 1 1 1 1 2 2 2 2 3 3 ...
# $ VAR : int 29 41 79 86 29 41 79 86 29 41 ...
# $ COUNTER: int 1 2 3 4 1 2 3 4 1 2 ...
# - attr(*, "vars")=List of 1
# ..$ : symbol ID
# - attr(*, "labels")='data.frame': 3 obs. of 1 variable:
# ..$ ID: Factor w/ 3 levels "a","b","c": 1 2 3
# ..- attr(*, "vars")=List of 1
# .. ..$ : symbol ID
# ..- attr(*, "drop")= logi TRUE
# - attr(*, "indices")=List of 3
# ..$ : int 0 1 2 3
# ..$ : int 4 5 6 7
# ..$ : int 8 9 10 11
# - attr(*, "drop")= logi TRUE
# - attr(*, "group_sizes")= int 4 4 4
# - attr(*, "biggest_group_size")= int 4
str(dt_ungrouped)
# Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 12 obs. of 3 variables:
# $ ID : Factor w/ 3 levels "a","b","c": 1 1 1 1 2 2 2 2 3 3 ...
# $ VAR : int 29 41 79 86 29 41 79 86 29 41 ...
# $ COUNTER: int 1 2 3 4 1 2 3 4 1 2 ...