宽到长的多列问题

时间:2019-08-19 00:00:55

标签: r reshape long-integer tidyr melt

我有这样的东西:

id    role1   Approved by Role1     role2          Approved by Role2
1      Amy        1/1/2019          David             4/4/2019
2      Bob        2/2/2019          Sara              5/5/2019
3      Adam       3/3/2019          Rachel            6/6/2019

我想要这样的东西:

id    Name      Role        Approved     
1      Amy      role1       1/1/2019         
2      Bob      role1       2/2/2019        
3      Adam     role1       3/3/2019  
1      David    role2       4/4/2019         
2      Sara     role2       5/5/2019        
3      Rachel   role2       6/6/2019       

我认为这样的事情会起作用

 melt(df,id.vars= id,
  measure.vars= list(c("role1", "role2"),c("Approved by Role1", "Approved by Role2")),
  variable.name= c("Role","Approved"),
  value.name= c("Name","Date"))

但是我遇到错误:测量在data:c(“ role1”,“ role2”),c(“ Role1批准”,“ Role2批准”)中找不到的变量

我也尝试用列数替换它,但是没有任何运气。

有什么建议吗?谢谢!

1 个答案:

答案 0 :(得分:1)

我真的很喜欢新的tidyr::pivot_longer()函数。它仍然仅在tidyr的开发版本中可用,但应尽快发布。首先,我将稍微清理一下列名,以便它们具有一致的结构:

> df
# A tibble: 3 x 5
     id name_role1 approved_role1 name_role2 approved_role2
  <dbl> <chr>      <chr>          <chr>      <chr>         
1     1 Amy        1/1/2019       David      4/4/2019      
2     2 Bob        2/2/2019       Sara       5/5/2019      
3     3 Adam       3/3/2019       Rachel     6/6/2019  

然后可以很容易地使用pivot_longer()转换为长格式:

library(tidyr)

df %>%
    pivot_longer(
        -id,
        names_to = c(".value", "role"),
        names_sep = "_"
    )

输出:

     id role  name   approved
  <dbl> <chr> <chr>  <chr>   
1     1 role1 Amy    1/1/2019
2     1 role2 David  4/4/2019
3     2 role1 Bob    2/2/2019
4     2 role2 Sara   5/5/2019
5     3 role1 Adam   3/3/2019
6     3 role2 Rachel 6/6/2019