宽到长的多列问题 [英] wide to long multiple columns issue
问题描述
我有这样的东西:
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
我认为类似的事情会起作用
I thought something like this would work
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"))
但是我遇到错误:测量在数据中找不到的变量:c("role1","role2"),c("Role1批准","Role2批准")
but i am getting Error: measure variables not found in data:c("role1", "role2"),c("Approved by Role1", "Approved by Role2")
我也尝试将其替换为列数,但没有任何运气.
I have tried replacing this with the number of the columns as well and haven't had any luck.
有什么建议吗??谢谢!
Any suggestions?? Thanks!
推荐答案
我真的很喜欢新的tidyr::pivot_longer()
函数.它仍然仅在tidyr
的开发版本中可用,但应尽快发布.首先,我将略微清理列名称,以使它们具有一致的结构:
I really like the new tidyr::pivot_longer()
function. It's still only available in the dev version of tidyr
, but should be released shortly. First I'm going to clean up the column names slightly, so they have a consistent structure:
> 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()
转换为长格式:
Then it's easy to convert to long format with 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
这篇关于宽到长的多列问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!