我有以下数据集,其中有多列对应:prompt1 with value1,prompt2 with value2,promptn to 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传播,但没有成功
答案 0 :(得分:1)
使用dplyr
和tidyr
中的函数的解决方案。
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)