将多行数据格式化为R中的单行

时间:2018-07-20 12:13:39

标签: r dplyr data.table reshape tidyr

我是一个奇怪的excel或csv格式的文件,我想将其作为数据帧导入R。问题是某些列有多个记录行,例如,数据如下:有三列和两行,但是工具列有多列,有没有一种方法可以格式化数据,所以我将仅使用多个工具(例如tool1,tool2等)进行记录

Task             Location  Tools 
Raising ticket   Alabama   sharepoint
                           word
                           oracle
Changing ticket  Seattle   word 
                           oracle

最终输出预期

Task             Location  Tools1   Tools2  Tools3
Raising ticket   Alabama   sharepoint   word    oracle
Changing ticket  Seattle   word         oracle

1 个答案:

答案 0 :(得分:2)

使用dplyrtidyr。您可以fill数据框,以便在每一行中包含“任务”和“位置”。然后group_by任务和mutate为每个组中的每个任务添加一个id列。然后使用spread将新创建的id列分布到多个列中。

library(dplyr)
library(tidyr)
df <- data.frame(Task = c("Raising ticket","","","Changing ticket",""), Location = c("Alabama","","","Seattle",""), Tools = c("sharepoint","word","oracle","word","oracle"))
df[df==""]  <- NA
df %>%
  fill(Task,Location) %>%
  group_by(Task) %>%
  mutate(id = paste0("Tools",row_number())) %>%
  spread(id, Tools)

# A tibble: 2 x 5
# Groups: Task [2]
#  Task            Location Tools1     Tools2 Tools3
#   <fct>           <fct>    <fct>      <fct>  <fct> 
# 1 Changing ticket Seattle  word       oracle <NA>  
# 2 Raising ticket  Alabama  sharepoint word   oracle