英文:
Order elements into different columns using separate()
问题
我有一些来自“选择所有适用”的调查数据,现在我有类似以下的数据:
ID <- c(1,2,3,4,5)
answer <- c("apple, orange", "pinneaple, apple", "orange, pinneaple", "pinneaple, orange","apple")
df <- data.frame(ID,answer)
> df
ID answer
1 1 apple, orange
2 2 pinneaple, apple
3 3 orange, pinneaple
4 4 pinneaple, orange
5 5 apple
现在,我正在尝试将其分开并展开结果,为此我正在使用dplyr
中的separate
,但它只部分实现了我想要的效果:
df %>%
separate(answer, into = c("ans1", "ans2","ans3"), sep = ", ")
> df
ID ans1 ans2 ans3
1 1 apple orange <NA>
2 2 pinneaple apple <NA>
3 3 orange pinneaple <NA>
4 4 pinneaple orange <NA>
5 5 apple <NA> <NA>
现在,我想要按顺序排列响应。在示例中,将ans1
分配给apple
等等,直到我们最终得到这样的结果:
> target_df
ID ans1 ans2 ans3
1 1 apple orange <NA>
2 2 apple <NA> pinneaple
3 3 <NA> orange pinneaple
4 4 <NA> orange pinneaple
5 5 apple <NA> <NA>
有没有办法向separate
函数添加此排序属性?
英文:
I have some data from a "select all that apply" survey data and I now have something like this:
ID <- c(1,2,3,4,5)
answer <- c("apple, orange", "pinneaple, apple", "orange, pinneaple", "pinneaple, orange","apple")
df <- data.frame(ID,answer)
> df
ID answer
1 1 apple, orange
2 2 pinneaple, apple
3 3 orange, pinneaple
4 4 pinneaple, orange
5 5 apple
Now, I am trying to separate this and spread the results, for this I am using separate
from dplyr
and it does only partially what I am looking for:
df %>%
separate(answer, into = c("ans1", "ans2","ans3"), sep = ", ")
> df
ID ans1 ans2 ans3
1 1 apple orange <NA>
2 2 pinneaple apple <NA>
3 3 orange pinneaple <NA>
4 4 pinneaple orange <NA>
5 5 apple <NA> <NA>
Now, I would like to have the responses ordered. In the example that would be assigning ans1
to apple
and so on, until we would end up with something like this:
> target_df
ID ans1 ans2 ans3
1 1 apple orange <NA>
2 2 apple <NA> pinneaple
3 3 <NA> orange pinneaple
4 4 <NA> orange pinneaple
5 5 apple <NA> <NA>
Is there a way to add this order attribute to the separate
function?
答案1
得分: 1
一个可能的方法如下,尽管它不使用 separate
:
library(dplyr)
library(tidyr)
library(stringr)
ID <- c(1,2,3,4,5)
answer <- c("apple, orange", "pinneaple, apple", "orange, pinneaple", "pinneaple, orange","apple")
df1 <- data.frame(ID,answer)
df1 |>
separate_rows(answer) |>
mutate(value = answer,
value = str_replace_all(value, c("apple" = "ans1", "orange" = "ans2", "pinneaple" = "ans3"))) |>
pivot_wider(names_from = value, values_from = answer)
创建于2023-07-04,使用 reprex v2.0.2
英文:
One approach could be as follows, although it does not use separate
:
library(dplyr)
library(tidyr)
library(stringr)
ID <- c(1,2,3,4,5)
answer <- c("apple, orange", "pinneaple, apple", "orange, pinneaple", "pinneaple, orange","apple")
df1 <- data.frame(ID,answer)
df1 |>
separate_rows(answer) |>
mutate(value = answer,
value = str_replace_all(value, c("apple" = "ans1", "orange" = "ans2", "pinneaple" = "ans3"))) |>
pivot_wider(names_from = value, values_from = answer)
#> # A tibble: 5 × 4
#> ID ans1 ans2 ans3
#> <dbl> <chr> <chr> <chr>
#> 1 1 apple orange <NA>
#> 2 2 apple <NA> pinneaple
#> 3 3 <NA> orange pinneaple
#> 4 4 <NA> orange pinneaple
#> 5 5 apple <NA> <NA>
<sup>Created on 2023-07-04 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论