我有一个像这样的数据集
ID|Type
1 "Basketball"
1 "Baseball"
2 "Basketball"
2 "Football"
3 "Boxing"
4 "Boxing"
4 "Wrestling"
4 "Handball"
4 "Hockey"我想创建一个如下所示的数据集
ID| Type|observation
1 "Basketball" 1
1 "Baseball" 2
2 "Basketball" 1
2 "Football" 2
3 "Boxing" 1
4 "Boxing" 1
4 "Wrestling" 2
4 "Handball" 3
4 "Hockey" 4我被困在这部分之后,我试着做这个
df %>%
group_by(ID) %>%
1:nrow(df)发布于 2021-05-14 18:45:03
我们可以使用row_number()) (它是在我们编辑之前首先在这里发布的,唯一的问题是包含类型的分组,并且没有在前面进行测试)
library(dplyr)
df %>%
group_by(ID) %>%
mutate(observation = match(Type, unique(Type))) %>%
ungroup-output
# A tibble: 9 x 3
# ID Type observation
# <int> <chr> <int>
#1 1 Basketball 1
#2 1 Baseball 2
#3 2 Basketball 1
#4 2 Football 2
#5 3 Boxing 1
#6 4 Boxing 1
#7 4 Wrestling 2
#8 4 Handball 3
#9 4 Hockey 4或者使用factor
df %>%
group_by(ID) %>%
mutate(observation = as.integer(factor(Type, levels = unique(Type)))) %>%
ungroup或使用1:n()
df %>%
group_by(ID) %>%
mutate(observation = 1:n())或者使用base R
df$observation <- with(df, ave(seq_along(ID), ID, FUN = seq_along))数据
df <- structure(list(ID = c(1L, 1L, 2L, 2L, 3L, 4L, 4L, 4L, 4L),
Type = c("Basketball",
"Baseball", "Basketball", "Football", "Boxing", "Boxing", "Wrestling",
"Handball", "Hockey")), class = "data.frame", row.names = c(NA,
-9L))发布于 2021-05-14 18:55:33
可以使用row_number()添加组行号。
df %>%
group_by(ID) %>%
mutate(observation = row_number())mutate是用于添加或修改列的通用dplyr函数。
https://stackoverflow.com/questions/67539509
复制相似问题