将元素按照 separate() 函数分成不同的列。

huangapple go评论96阅读模式
英文:

Order elements into different columns using separate()

问题

我有一些来自“选择所有适用”的调查数据,现在我有类似以下的数据:

  1. ID <- c(1,2,3,4,5)
  2. answer <- c("apple, orange", "pinneaple, apple", "orange, pinneaple", "pinneaple, orange","apple")
  3. df <- data.frame(ID,answer)
  4. > df
  5. ID answer
  6. 1 1 apple, orange
  7. 2 2 pinneaple, apple
  8. 3 3 orange, pinneaple
  9. 4 4 pinneaple, orange
  10. 5 5 apple

现在,我正在尝试将其分开并展开结果,为此我正在使用dplyr中的separate,但它只部分实现了我想要的效果:

  1. df %>%
  2. separate(answer, into = c("ans1", "ans2","ans3"), sep = ", ")
  3. > df
  4. ID ans1 ans2 ans3
  5. 1 1 apple orange <NA>
  6. 2 2 pinneaple apple <NA>
  7. 3 3 orange pinneaple <NA>
  8. 4 4 pinneaple orange <NA>
  9. 5 5 apple <NA> <NA>

现在,我想要按顺序排列响应。在示例中,将ans1分配给apple等等,直到我们最终得到这样的结果:

  1. > target_df
  2. ID ans1 ans2 ans3
  3. 1 1 apple orange <NA>
  4. 2 2 apple <NA> pinneaple
  5. 3 3 <NA> orange pinneaple
  6. 4 4 <NA> orange pinneaple
  7. 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:

  1. ID &lt;- c(1,2,3,4,5)
  2. answer &lt;- c(&quot;apple, orange&quot;, &quot;pinneaple, apple&quot;, &quot;orange, pinneaple&quot;, &quot;pinneaple, orange&quot;,&quot;apple&quot;)
  3. df &lt;- data.frame(ID,answer)
  4. &gt; df
  5. ID answer
  6. 1 1 apple, orange
  7. 2 2 pinneaple, apple
  8. 3 3 orange, pinneaple
  9. 4 4 pinneaple, orange
  10. 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:

  1. df %&gt;%
  2. separate(answer, into = c(&quot;ans1&quot;, &quot;ans2&quot;,&quot;ans3&quot;), sep = &quot;, &quot;)
  3. &gt; df
  4. ID ans1 ans2 ans3
  5. 1 1 apple orange &lt;NA&gt;
  6. 2 2 pinneaple apple &lt;NA&gt;
  7. 3 3 orange pinneaple &lt;NA&gt;
  8. 4 4 pinneaple orange &lt;NA&gt;
  9. 5 5 apple &lt;NA&gt; &lt;NA&gt;

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:

  1. &gt; target_df
  2. ID ans1 ans2 ans3
  3. 1 1 apple orange &lt;NA&gt;
  4. 2 2 apple &lt;NA&gt; pinneaple
  5. 3 3 &lt;NA&gt; orange pinneaple
  6. 4 4 &lt;NA&gt; orange pinneaple
  7. 5 5 apple &lt;NA&gt; &lt;NA&gt;

Is there a way to add this order attribute to the separate function?

答案1

得分: 1

一个可能的方法如下,尽管它不使用 separate

  1. library(dplyr)
  2. library(tidyr)
  3. library(stringr)
  4. ID <- c(1,2,3,4,5)
  5. answer <- c("apple, orange", "pinneaple, apple", "orange, pinneaple", "pinneaple, orange","apple")
  6. df1 <- data.frame(ID,answer)
  7. df1 |&gt;
  8. separate_rows(answer) |&gt;
  9. mutate(value = answer,
  10. value = str_replace_all(value, c("apple" = "ans1", "orange" = "ans2", "pinneaple" = "ans3"))) |&gt;
  11. 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:

  1. library(dplyr)
  2. library(tidyr)
  3. library(stringr)
  4. ID &lt;- c(1,2,3,4,5)
  5. answer &lt;- c(&quot;apple, orange&quot;, &quot;pinneaple, apple&quot;, &quot;orange, pinneaple&quot;, &quot;pinneaple, orange&quot;,&quot;apple&quot;)
  6. df1 &lt;- data.frame(ID,answer)
  7. df1 |&gt;
  8. separate_rows(answer) |&gt;
  9. mutate(value = answer,
  10. value = str_replace_all(value, c(&quot;apple&quot; = &quot;ans1&quot;, &quot;orange&quot; = &quot;ans2&quot;, &quot;pinneaple&quot; = &quot;ans3&quot;))) |&gt;
  11. pivot_wider(names_from = value, values_from = answer)
  12. #&gt; # A tibble: 5 &#215; 4
  13. #&gt; ID ans1 ans2 ans3
  14. #&gt; &lt;dbl&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
  15. #&gt; 1 1 apple orange &lt;NA&gt;
  16. #&gt; 2 2 apple &lt;NA&gt; pinneaple
  17. #&gt; 3 3 &lt;NA&gt; orange pinneaple
  18. #&gt; 4 4 &lt;NA&gt; orange pinneaple
  19. #&gt; 5 5 apple &lt;NA&gt; &lt;NA&gt;

<sup>Created on 2023-07-04 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年7月5日 00:33:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76614500.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定