如何将具有不同类别级别的多个列转换为宽格式?

How to convert multiple columns with various category levels to wide format?

我有以下一组数据,其中包含多个对应的列:prompt1 与 value1,prompt2 与 value2,promptn 与 valuen。每个提示最多包含 3 个不同级别的类别。

df.1 <- data.frame(prompt1         = c('date', 'company', 'invoice'),
               value1              = c('2017-01-01', 'Tellabs', '23845'),
               prompt2             = c('code', 'city', 'item'),
               value2              = c('B34', 'Ilinois', 'SER0000066'),
               stringsAsFactors    = FALSE)

所需的数据帧为:

df.2 <- data.frame(date              = '2017-01-010',
                   company           = 'Tellabs',
                   invoice           = '23845',
                   code              = 'B34',
                   city              = 'Ilinois',
                   item              = 'SER0000066',
                   stringsAsFactors  = FALSE)

我试过从 tidyr 传播但没有成功

使用 dplyrtidyr 中函数的解决方案。

library(dplyr)
library(tidyr)

df.2 <- df.1 %>%
  gather(Prompt, Column, contains("prompt")) %>%
  gather(Value, Content, contains("value")) %>%
  filter(sub("prompt", "", Prompt) == sub("value", "", Value)) %>%
  select(Column, Content) %>%
  spread(Column, Content) %>%
  select(date, company, invoice, code, city, item)