繁体   English   中英

R Pivot 多列从宽到长

[英]R Pivot multiple columns from wide to long

当我需要 pivot 多列(但不是所有列)从宽到长时,我不确定如何使用 pivot 更长的时间。

起始数据:

    dat <- tibble(aColumn = c("stuff", "stuff", "stuff"),
               anotherColumn = c("more stuff", "more stuff", "more stuff"),
               `Day a Time` = c("12:00", "13:00", "14:00"),
               `Day a Value` = c("R", "I", "O"),
               `Day b Time` = c("12:45", "14:00", "15:35"),
               `Day b Value` = c("R", "P", "O"))

最终数据:

desiredDat <- tibble(aColumn = rep("stuff", 6),
       anotherColumn = rep("more stuff", 6),
       DayID = c("a", "a", "a", "b", "b", "b"),
       DayTime = c("12:00", "13:00", "14:00", "12:45", "14:00", "15:35"),
       DayValue = c("R","I","O","R","P","O"))

有谁知道我该怎么做?

重新排列列名 substring 后,这里有一个带有pivot_longer的选项。我们通过重新排列单词rename以“Day”开头的列,即 substring 列名中的“a”、“b”移动到带有分隔符的末尾_通过捕获这些词 ( (\\w+) ) 然后替换str_replace ,改变反向引用顺序 ( \\1 , \\2等)

library(dplyr)
library(stringr)
library(tidyr)
dat %>%
     rename_at(vars(starts_with('Day')), 
       ~ str_replace(., '(\\w+) (\\w+) (\\w+)', "\\1\\3_\\2")) %>% 
     pivot_longer(cols = starts_with('Day'), 
           names_to = c( ".value", "DayID"), names_sep="_")

-输出

# A tibble: 6 x 5
#  aColumn anotherColumn DayID DayTime DayValue
#  <chr>   <chr>         <chr> <chr>   <chr>   
#1 stuff   more stuff    a     12:00   R       
#2 stuff   more stuff    b     12:45   R       
#3 stuff   more stuff    a     13:00   I       
#4 stuff   more stuff    b     14:00   P       
#5 stuff   more stuff    a     14:00   O       
#6 stuff   more stuff    b     15:35   O    

在 base R 中,你可以这样做:

dat1 <- setNames(dat, sub(" (\\w+) (\\w+)$",'\\2 \\1', names(dat)))

tms <- unique(sub('\\w+ (\\w) \\w+', '\\1', names(df)[-(1:2)]))

reshape(dat1, -(1:2), dir='long', times = tms, sep=' ')

    aColumn anotherColumn time DayTime DayValue id
1.a   stuff    more stuff    a   12:00        R  1
2.a   stuff    more stuff    a   13:00        I  2
3.a   stuff    more stuff    a   14:00        O  3
1.b   stuff    more stuff    b   12:45        R  1
2.b   stuff    more stuff    b   14:00        P  2
3.b   stuff    more stuff    b   15:35        O  3

然后你可以将列名更改为你想要的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM