如何在r中按日期合并/堆叠观测值



我有一个像

这样的数据帧
date             X1 X2 X3
4/16/2019 0:00   1  2  3
4/16/2019 7:00   1  2  3
4/172019 0:00    1  2  3
4/17/2019 7:00   1  2  3

我想要得到

date        time     X1      X2      X3
4/16/2019   c(0,7)   c(1,1)  c(2,2)  c(3,3)
4/17/2019   c(0,7)   c(1,1)  c(2,2)  c(3,3)

其中X1是一个列表,X1[[1]]是一个向量,即c(1,1)

是否有一种有效的方法来实现这一点?谢谢你!

将'date'拆分为'date', 'time'列在空格处(\s+),按'date'分组,然后summariseacross所有列通过将它们包装在list

library(dplyr)
library(tidyr)
library(stringr)
df1 %>%   
separate(date, into = c('date', 'time'), sep = '\s+') %>%
mutate(time = as.numeric(str_replace(time, ":", ".")) %>%
group_by(date) %>%
summarise(across(everything(), ~ list(.)))

与产出

# A tibble: 2 × 5
date      time      X1        X2        X3       
<chr>     <list>    <list>    <list>    <list>   
1 4/16/2019 <dbl [2]> <int [2]> <int [2]> <int [2]>
2 4/17/2019 <dbl [2]> <int [2]> <int [2]> <int [2]>

数据
df1 <- structure(list(date = c("4/16/2019 0:00", "4/16/2019 7:00", 
"4/17/2019 0:00", 
"4/17/2019 7:00"), X1 = c(1L, 1L, 1L, 1L), X2 = c(2L, 2L, 2L, 
2L), X3 = c(3L, 3L, 3L, 3L)), 
class = "data.frame", row.names = c(NA, 
-4L))

你可以这样做:逻辑:

  1. 单独的日期和时间列(除了separate,如akrun已经提供的)
  2. listlambda paste总结across(注意summarise
  3. 中的.names参数)
  4. 再次使用acrosslambda paste0
library(dplyr)
library(readr)
df %>% 
mutate(date = mdy_hm(date)) %>% 
mutate(time = parse_number(sprintf("%02d", hour(date))), .before=2,
date = as.Date(ymd_hms(date))) %>% 
group_by(date) %>% 
summarise(across(everything(), list(~paste(.,collapse=",")), .names="{col}")) %>% 
mutate(across(-date, ~paste0("c(",.,")")))
date       time   X1     X2     X3    
<date>     <chr>  <chr>  <chr>  <chr> 
1 2019-04-16 c(0,7) c(1,1) c(2,2) c(3,3)
2 2019-04-17 c(0,7) c(1,1) c(2,2) c(3,3)

相关内容

  • 没有找到相关文章

最新更新