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

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

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 &lt;- c(1,2,3,4,5)
answer &lt;- c(&quot;apple, orange&quot;, &quot;pinneaple, apple&quot;, &quot;orange, pinneaple&quot;, &quot;pinneaple, orange&quot;,&quot;apple&quot;)

df &lt;- data.frame(ID,answer)

&gt; 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 %&gt;% 
  separate(answer, into = c(&quot;ans1&quot;, &quot;ans2&quot;,&quot;ans3&quot;), sep = &quot;, &quot;)

&gt; df
  ID      ans1      ans2 ans3
1  1     apple    orange &lt;NA&gt;
2  2 pinneaple     apple &lt;NA&gt;
3  3    orange pinneaple &lt;NA&gt;
4  4 pinneaple    orange &lt;NA&gt;
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:

&gt; target_df
  ID  ans1   ans2      ans3
1  1 apple orange      &lt;NA&gt;
2  2 apple   &lt;NA&gt; pinneaple
3  3  &lt;NA&gt; orange pinneaple
4  4  &lt;NA&gt; orange pinneaple
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

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 |&gt; 
  separate_rows(answer) |&gt; 
  mutate(value = answer, 
         value = str_replace_all(value, c("apple" = "ans1", "orange" = "ans2", "pinneaple" = "ans3"))) |&gt; 
  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 &lt;- c(1,2,3,4,5)
answer &lt;- c(&quot;apple, orange&quot;, &quot;pinneaple, apple&quot;, &quot;orange, pinneaple&quot;, &quot;pinneaple, orange&quot;,&quot;apple&quot;)

df1 &lt;- data.frame(ID,answer)

df1 |&gt; 
  separate_rows(answer) |&gt; 
  mutate(value = answer, 
         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; 
  pivot_wider(names_from = value, values_from = answer)
#&gt; # A tibble: 5 &#215; 4
#&gt;      ID ans1  ans2   ans3     
#&gt;   &lt;dbl&gt; &lt;chr&gt; &lt;chr&gt;  &lt;chr&gt;    
#&gt; 1     1 apple orange &lt;NA&gt;     
#&gt; 2     2 apple &lt;NA&gt;   pinneaple
#&gt; 3     3 &lt;NA&gt;  orange pinneaple
#&gt; 4     4 &lt;NA&gt;  orange pinneaple
#&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:

确定