R,在更改列名称的同时从宽向长旋转

标签 r tidyverse

我有这样的数据:

df<-structure(list(fname = c("Linda", "Bob"), employee_number = c("00000123456", 
"654321"), job_role = c("Dept Research Admin", "Research Regulatory Assistant"
), ActiveAccount = c("Yes", "Yes"), CanAccess = c("No", "No"), 
    oncore_roles___1 = c(1, 0), oncore_roles___2 = c(1, 0), oncore_roles___3 = c(1, 
    0), oncore_roles___4 = c(0, 0), oncore_roles___5 = c(0, 1
    ), oncore_roles___6 = c(0, 0), oncore_roles___7 = c(0, 1), 
    oncore_roles___8 = c(0, 0), oncore_roles___9 = c(0, 0), oncore_roles___10 = c(0, 
    0), oncore_roles___11 = c(0, 0), oncore_roles___12 = c(0, 
    1), oncore_roles___13 = c(0, 0), oncore_roles___14 = c(0, 
    0), oncore_roles___15 = c(0, 0), oncore_roles___16 = c(0, 
    0), oncore_roles___17 = c(0, 0)), row.names = c(NA, -2L), class = c("tbl_df", 
"tbl", "data.frame"))

以“oncore Roles”开头的列均来自此多项选择调查选项: enter image description here

其中 oncore_roles_1 代表“日历构建”,oncore_roles_5 代表“首席调查员”等... IE。如果鲍勃在 Oncore_roles_5 中标记为“1”,那么他是一名首席研究员,如果他在所有其他“oncore_roles”列中标记为 0...他不是那些人。

我需要调整我的数据,使其更长,并且只有一列“Oncore Roles”,其中包含说明该人拥有的角色的文本,每个角色各占一行。因此,如果鲍勃扮演三个角色,他就会得到三句几乎相同的台词。除了 oncore_roles 变量之外,一切都相同。

我知道这可能是pivot_longer的某个版本,但诀窍(我为什么问)是我需要删除所有的零。 IE。对于这个特定的数据,我会留下这个:

enter image description here

谢谢!

最佳答案

如果您构建一个小型的核心角色查找表,例如 roles,您可以执行以下操作:

df %>%
  pivot_longer(cols = -(fname:CanAccess),names_prefix = "oncore_roles___",names_to = "id") %>% 
  filter(value==1) %>% 
  mutate(id=as.numeric(id)) %>% 
  left_join(roles, by="id") %>% 
  select(-(id:value))

输出(请注意,我的 roles 只有前 5 个角色,但您可以使其更长,然后您可以使用 inner_join(),而不是 left_join():

  fname employee_number job_role                      ActiveAccount CanAccess Oncore_role           
  <chr> <chr>           <chr>                         <chr>         <chr>     <chr>                 
1 Linda 00000123456     Dept Research Admin           Yes           No        Calendar Build        
2 Linda 00000123456     Dept Research Admin           Yes           No        Protocol Management   
3 Linda 00000123456     Dept Research Admin           Yes           No        Subject Management    
4 Bob   654321          Research Regulatory Assistant Yes           No        Principal Investigator
5 Bob   654321          Research Regulatory Assistant Yes           No        NA                    
6 Bob   654321          Research Regulatory Assistant Yes           No        NA            

角色:


roles =tibble(
  id = 1:5,
  Oncore_role = c(
    "Calendar Build",
    "Protocol Management",
    "Subject Management",
    "Financial",
    "Principal Investigator"
))

关于R,在更改列名称的同时从宽向长旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71592957/

相关文章:

r - 数据表和开发工具 : install_github error - function works locally but not after installing package from github

r - 给定向量和 0 沿反对角线排列生成 5x5 矩阵

R igraph - 保存布局?

r - 阻止 ggplot2 中的文本标签重叠

r - Tidyeval in own functions in own functions inside own functions with the pipe 管道

r - 如何将函数应用于 data.table 的行子集,其中每次调用都返回一个 data.table

r - 转换数据框中的列表调整化合物名称

r - 当达到空白值时 Purrr 函数编程错误

R:将数据框列名称与数字连接

r - 如何调整 NumericVector 的大小?