如何将编号的列彼此耦合而从宽变长?

时间:2018-12-18 20:45:15

标签: r dplyr reshape

我有一个看起来像这样的数据集:

phrase      wo1sp     wo2sp     wo3sp     wo1sc     wo2sc     wo3sc
hello       dan       mark      todd      10        5         4
hello       mark      dan       chris     8         9         4
goodbye     mark      dan       kev       2         4         10
what        kev       dan       mark      4         5         5

我想将其更改为以下内容:

phrase      sp      sc
hello       dan     10 
hello       mark    5
hello       todd    4
hello       mark    8
hello       dan     9
hello       chris   4
goodbye     mark    2
goodbye     dan     4
goodbye     kev     10
what        kev     4
what        dan     5
what        mark    5

所以,我知道这里要做的第一件事是group_by(phrase)。我不确定如何将sp1sc1关联,如何将sp2sc2关联,等等,然后将它们分别放入自己的行中。我已经看到了使用reshapetidy的一些相似的东西,但是它们并不依赖于 coupled 列。我基本上只是想折叠列名称中的数字。

我有一个要求:当您回答时,您介意解释代码本身的作用吗?我在StackExchange上搜索的许多内容都提供了一种看似深奥的解决方案,没有解释发生了什么。

1 个答案:

答案 0 :(得分:1)

library("tidyverse")

test_set = tribble(~phrase,      ~wo1sp,     ~wo2sp,     ~wo3sp,     ~wo1sc,     ~wo2sc,     ~wo3sc,
                   "hello",       "dan",       "mark",      "todd",      10,        5,         4,
                   "goodbye",     "mark",      "dan",       "kev",       2,         4,         10,
                   "what",        "kev",       "dan",       "mark",      4,         5,         5)

test_set %>% 
  gather(key = col, value = val, -phrase) %>% 
  separate(col = col, into = c("num", "suffix"), sep = 3) %>% 
  spread(key = suffix, value = val) %>% 
  mutate(sc = as.numeric(sc)) %>% 
  select(-num)

编辑:我想没有必要将col分成三列,只需执行sep = 3