R:几个时间戳列合并到时间戳系列
R: Several Timestamp columns merged to Timestamp Series
一个数据框包含多个 TIMESTAMP 列。
# Create simple example
df <- cbind.data.frame(A=c("A","B","C")
,B=c(1,2,3)
,Timestamp_1=c(as.POSIXct(NA),as.POSIXct("2018-05-04 00:19:41"),as.POSIXct("2018-07-31 22:09:10"))
,Timestamp_2=c(as.POSIXct("2018-05-04 00:18:45"),as.POSIXct("2018-05-05 00:18:43"),as.POSIXct("2018-06-05 00:00:01"))
,Timestamp_3=c(as.POSIXct("2018-05-04 00:19:13"),as.POSIXct("2018-05-05 00:17:00"),as.POSIXct("2018-05-06 00:18:41"))
,C=c("Dog","Cat","Mouse")
)
df
A B Timestamp_1 Timestamp_2 Timestamp_3 C
1 A 1 <NA> 2018-05-04 00:18:45 2018-05-04 00:19:13 Dog
2 B 2 2018-05-04 00:19:41 2018-05-05 00:18:43 2018-05-05 00:17:00 Cat
3 C 3 2018-07-31 22:09:10 2018-06-05 00:00:01 2018-05-06 00:18:41 Mouse
合并 TIMESTAMP 列的结果需要类似于
df_Result
A B Timestamp_ALL C
1 A 1 <NA> Dog
2 A 1 2018-05-04 00:18:45 Dog
3 A 1 2018-05-04 00:19:13 Dog
4 B 2 2018-05-04 00:19:41 Cat
5 B 2 2018-05-05 00:17:00 Cat
6 B 2 2018-05-05 00:18:43 Cat
7 C 3 2018-05-06 00:18:41 Mouse
8 C 3 2018-06-05 00:00:01 Mouse
9 C 3 2018-07-31 22:09:10 Mouse
如何以优雅高效的方式做到这一点?
感谢一百万的任何建议和想法!
这是一个 tidyverse
使用 tidyr::gather
的解决方案
library(tidyverse)
df %>%
gather(key, Timestamp_ALL, -A, -B, -C) %>%
select(A, B, Timestamp_ALL, C, -key) %>%
arrange(A, B)
# A B Timestamp_ALL C
#1 A 1 <NA> Dog
#2 A 1 2018-05-04 00:18:45 Dog
#3 A 1 2018-05-04 00:19:13 Dog
#4 B 2 2018-05-04 00:19:41 Cat
#5 B 2 2018-05-05 00:18:43 Cat
#6 B 2 2018-05-05 00:17:00 Cat
#7 C 3 2018-07-31 22:09:10 Mouse
#8 C 3 2018-06-05 00:00:01 Mouse
#9 C 3 2018-05-06 00:18:41 Mouse
说明:gather
将 data.frame
从宽转换为长,剩下的只是使用 select
选择相关列,然后按 A
排序条目然后 B
与 arrange
。
一个数据框包含多个 TIMESTAMP 列。
# Create simple example
df <- cbind.data.frame(A=c("A","B","C")
,B=c(1,2,3)
,Timestamp_1=c(as.POSIXct(NA),as.POSIXct("2018-05-04 00:19:41"),as.POSIXct("2018-07-31 22:09:10"))
,Timestamp_2=c(as.POSIXct("2018-05-04 00:18:45"),as.POSIXct("2018-05-05 00:18:43"),as.POSIXct("2018-06-05 00:00:01"))
,Timestamp_3=c(as.POSIXct("2018-05-04 00:19:13"),as.POSIXct("2018-05-05 00:17:00"),as.POSIXct("2018-05-06 00:18:41"))
,C=c("Dog","Cat","Mouse")
)
df
A B Timestamp_1 Timestamp_2 Timestamp_3 C
1 A 1 <NA> 2018-05-04 00:18:45 2018-05-04 00:19:13 Dog
2 B 2 2018-05-04 00:19:41 2018-05-05 00:18:43 2018-05-05 00:17:00 Cat
3 C 3 2018-07-31 22:09:10 2018-06-05 00:00:01 2018-05-06 00:18:41 Mouse
合并 TIMESTAMP 列的结果需要类似于
df_Result
A B Timestamp_ALL C
1 A 1 <NA> Dog
2 A 1 2018-05-04 00:18:45 Dog
3 A 1 2018-05-04 00:19:13 Dog
4 B 2 2018-05-04 00:19:41 Cat
5 B 2 2018-05-05 00:17:00 Cat
6 B 2 2018-05-05 00:18:43 Cat
7 C 3 2018-05-06 00:18:41 Mouse
8 C 3 2018-06-05 00:00:01 Mouse
9 C 3 2018-07-31 22:09:10 Mouse
如何以优雅高效的方式做到这一点? 感谢一百万的任何建议和想法!
这是一个 tidyverse
使用 tidyr::gather
library(tidyverse)
df %>%
gather(key, Timestamp_ALL, -A, -B, -C) %>%
select(A, B, Timestamp_ALL, C, -key) %>%
arrange(A, B)
# A B Timestamp_ALL C
#1 A 1 <NA> Dog
#2 A 1 2018-05-04 00:18:45 Dog
#3 A 1 2018-05-04 00:19:13 Dog
#4 B 2 2018-05-04 00:19:41 Cat
#5 B 2 2018-05-05 00:18:43 Cat
#6 B 2 2018-05-05 00:17:00 Cat
#7 C 3 2018-07-31 22:09:10 Mouse
#8 C 3 2018-06-05 00:00:01 Mouse
#9 C 3 2018-05-06 00:18:41 Mouse
说明:gather
将 data.frame
从宽转换为长,剩下的只是使用 select
选择相关列,然后按 A
排序条目然后 B
与 arrange
。