我的数据库很大,很多数据都是重复的,我想为这些数据生成两个新列。
这里是数据摘要
df <- tribble(~ID, ~Code,
1, "p",
1, "p",
2, "f",
3, "p",
2, "p",
3, "f",
4, "f",
4, "p",
6, "p")
我想获取以下数据:
Out1 Out2
1 p p
2 f p
3 p f
4 f p
5 p NA
答案 0 :(得分:0)
一种方法是:
df %>%
group_by(ID) %>%
mutate(temp = paste0("Out", seq_along(ID))) %>%
spread(temp, Code)
ID Out1 Out2
<dbl> <chr> <chr>
1 1 p p
2 2 f p
3 3 p f
4 4 f p
5 6 p <NA>
如果您还想更改ID:
df %>%
group_by(ID) %>%
mutate(temp = paste0("Out", seq_along(ID))) %>%
spread(temp, Code) %>%
ungroup() %>%
mutate(ID = seq_along(ID))
ID Out1 Out2
<int> <chr> <chr>
1 1 p p
2 2 f p
3 3 p f
4 4 f p
5 5 p <NA>