从行值转换为ID列的列

时间:2019-07-01 20:09:26

标签: r dataframe dplyr

我有此数据:

    IconButton(
        icon: Icon(Icons.date_range),
        onPressed: _selectDate(context)),

,我想要这样的输出:

df = data.frame(id = c(1,2,3), title = c("Google-Amazon","Amazon-Google-Yahoo","Yahoo-IMDF"), status = c("Open-Open", "Wait-Close-Open", "Close-Open"))

直到现在我一直尝试的是:

  id Google Amazon Yahoo IMDF
   1   Open   Open    NA   NA
   2  Close   Wait  Open   NA
   3     NA     NA Close Open

那之后我该怎么做?

1 个答案:

答案 0 :(得分:1)

一种选择是使用separate_rows拆分“标题”,“状态”的行,然后执行spread将其形状从“长”整形为“宽”

library(tidyverse)
df %>%
    separate_rows(title, status) %>%
    spread(title, status)
#  id Amazon Google IMDF Yahoo
#1  1   Open   Open <NA>  <NA>
#2  2   Wait  Close <NA>  Open
#3  3   <NA>   <NA> Open Close