[英]pivot_wider with duplicates
I have the following df我有以下 df
df <- structure(list(ID = c(1, 1, 1, 1, 1, 2, 2, 2, 2), value = c("p",
"p", "p1", "p2", "p3", "a", "b", "c", "d"), i1 = c(1, 1, 1, 1,
1, 1, 1, 1, 1)), row.names = c(NA, -9L), class = c("tbl_df",
"tbl", "data.frame"))
ID value i1 <dbl> <chr> <dbl> 1 1 p 1 2 1 p 1 3 1 p1 1 4 1 p2 1 5 1 p3 1 6 2 a 1 7 2 b 1 8 2 c 1 9 2 d 1
When I tried to pivot, I got an error saying there are duplicates.当我尝试旋转时,我收到一条错误消息,说有重复项。
df %>% pivot_wider(names_from = value, values_from = i1, values_fill = list(i1 = 0))
Warning message: Values in `i1` are not uniquely identified; output will contain list-cols. * Use `values_fn = list(i1 = list)` to suppress this warning. * Use `values_fn = list(i1 = length)` to identify where the duplicates arise * Use `values_fn = list(i1 = summary_fun)` to summarise duplicates
I would like to identify what values are repeating for each unique ID so I could filter.我想确定每个唯一 ID 重复的值,以便我可以过滤。 Or maybe I could remove duplicates during the pivot_wider() step.
或者我可以在 pivot_wider() 步骤中删除重复项。 The source code has name_repair which I set to "unique".
源代码有 name_repair,我将其设置为“唯一”。 Did not work!
不工作!
The ideal output is:理想的输出是:
p p1 p2 p3 abcd 1 1 1 1 1 0 0 0 0 2 0 0 0 0 1 1 1 1
I think in OP's attempt what they were trying to do was to remove duplicates and then pivot the data which can be done with distinct
and pivot_wider
.我认为在 OP 的尝试中,他们试图做的是删除重复项,然后旋转可以使用
distinct
和pivot_wider
完成的数据。
library(dplyr)
library(tidyr)
df %>%
distinct() %>%
pivot_wider(names_from = value, values_from = i1, values_fill = list(i1 = 0))
# A tibble: 2 x 9
# ID p p1 p2 p3 a b c d
# <dbl> <int> <int> <int> <int> <int> <int> <int> <int>
#1 1 1 1 1 1 0 0 0 0
#2 2 0 0 0 0 1 1 1 1
We can also use count
and pivot_wider
我们也可以使用
count
和pivot_wider
df %>%
count(ID, value) %>%
mutate(n = +(n > 0)) %>%
pivot_wider(names_from = value, values_from = n, values_fill = list(n = 0))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.