在 R 中组合来自两个不同传感器的时间序列数据
Combine timeseries data from two different sensors in R
我有来自 2 个独立重新编码的传感器的时间序列数据。它们都在不同的开始时间启动,并以不同的时间间隔记录数据。传感器 1 每 1 秒记录一次,而传感器 2 每 2 秒记录一次。我想将这两个数据集组合成一个数据框以便 ggplot。有人可以帮我吗?如果有比 ggplot 和 dataframes 更好的选择,请告诉我。谢谢您的帮助。我包含了示例数据(不是实际的,如果我没有在下面包含正确的示例,请告诉我):
dput(reading1)
structure(list(time = structure(c(-2209030842, -2209030841, -2209030840,
-2209030839, -2209030838, -2209030837, -2209030836, -2209030835,
-2209030834, -2209030833, -2209030832, -2209030831, -2209030830,
-2209030829, -2209030828, -2209030827, -2209030826, -2209030825,
-2209030824), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
`reading 1` = c(0.004, 0.003, 0.003, 0.013, 0.021, 0.008,
0.004, 0.005, 0.004, 0.007, 0.003, 0.004, 0.002, 0.003, 0.004,
0.004, 0.005, 0.001, 0.003)), class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -19L))
dput(reading2)
structure(list(Date = structure(c(-2209031012, -2209031009, -2209031007,
-2209031005, -2209031003, -2209030982, -2209030981, -2209030976,
-2209030974, -2209030972, -2209030970, -2209030949, -2209030882,
-2209030879, -2209030877, -2209030875, -2209030873, -2209030871,
-2209030850, -2209030849, -2209030838, -2209030816, -2209030814,
-2209030811, -2209030808, -2209030806, -2209030804, -2209030783,
-2209030782, -2209030780, -2209030778, -2209030775, -2209030773,
-2209030771, -2209030750, -2209030749, -2209030747, -2209030742,
-2209030740, -2209030738, -2209030717, -2209030705, -2209030684,
-2209030683, -2209030681, -2209030679, -2209030676, -2209030674,
-2209030672, -2209030651, -2209030650, -2209030648, -2209030646,
-2209030644, -2209030641, -2209030639), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), `reading 2` = c(8, 8, 8, 8, 8, 6,
6, 8, 8, 8, 8, 6, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6,
5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -56L))
您可以像这样使用 dplyr
组合它们:
combined <- bind_rows(
reading1 %>% rename(reading = `reading 1`) %>% mutate(sensor = 1),
reading2 %>% rename(reading = `reading 2`, time = Date) %>%
mutate(sensor = 2)) %>%
arrange(time)
combined
#> # A tibble: 75 x 3
#> time reading sensor
#> <dttm> <dbl> <dbl>
#> 1 1899-12-31 12:16:28 8 2
#> 2 1899-12-31 12:16:31 8 2
#> 3 1899-12-31 12:16:33 8 2
#> 4 1899-12-31 12:16:35 8 2
#> 5 1899-12-31 12:16:37 8 2
#> 6 1899-12-31 12:16:58 6 2
#> 7 1899-12-31 12:16:59 6 2
#> 8 1899-12-31 12:17:04 8 2
#> 9 1899-12-31 12:17:06 8 2
#> 10 1899-12-31 12:17:08 8 2
#> # ... with 65 more rows
像这样使用长格式的数据可以更轻松地绘图,例如:
library(ggplot2)
ggplot(combined, aes(time, reading, color = factor(sensor))) +
geom_line(size = 1) +
theme_bw(base_size = 16) +
scale_color_brewer(palette = "Set1", name = "Sensor")
由 reprex package (v2.0.1)
创建于 2022-05-19
我有来自 2 个独立重新编码的传感器的时间序列数据。它们都在不同的开始时间启动,并以不同的时间间隔记录数据。传感器 1 每 1 秒记录一次,而传感器 2 每 2 秒记录一次。我想将这两个数据集组合成一个数据框以便 ggplot。有人可以帮我吗?如果有比 ggplot 和 dataframes 更好的选择,请告诉我。谢谢您的帮助。我包含了示例数据(不是实际的,如果我没有在下面包含正确的示例,请告诉我):
dput(reading1)
structure(list(time = structure(c(-2209030842, -2209030841, -2209030840,
-2209030839, -2209030838, -2209030837, -2209030836, -2209030835,
-2209030834, -2209030833, -2209030832, -2209030831, -2209030830,
-2209030829, -2209030828, -2209030827, -2209030826, -2209030825,
-2209030824), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
`reading 1` = c(0.004, 0.003, 0.003, 0.013, 0.021, 0.008,
0.004, 0.005, 0.004, 0.007, 0.003, 0.004, 0.002, 0.003, 0.004,
0.004, 0.005, 0.001, 0.003)), class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -19L))
dput(reading2)
structure(list(Date = structure(c(-2209031012, -2209031009, -2209031007,
-2209031005, -2209031003, -2209030982, -2209030981, -2209030976,
-2209030974, -2209030972, -2209030970, -2209030949, -2209030882,
-2209030879, -2209030877, -2209030875, -2209030873, -2209030871,
-2209030850, -2209030849, -2209030838, -2209030816, -2209030814,
-2209030811, -2209030808, -2209030806, -2209030804, -2209030783,
-2209030782, -2209030780, -2209030778, -2209030775, -2209030773,
-2209030771, -2209030750, -2209030749, -2209030747, -2209030742,
-2209030740, -2209030738, -2209030717, -2209030705, -2209030684,
-2209030683, -2209030681, -2209030679, -2209030676, -2209030674,
-2209030672, -2209030651, -2209030650, -2209030648, -2209030646,
-2209030644, -2209030641, -2209030639), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), `reading 2` = c(8, 8, 8, 8, 8, 6,
6, 8, 8, 8, 8, 6, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6,
5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -56L))
您可以像这样使用 dplyr
组合它们:
combined <- bind_rows(
reading1 %>% rename(reading = `reading 1`) %>% mutate(sensor = 1),
reading2 %>% rename(reading = `reading 2`, time = Date) %>%
mutate(sensor = 2)) %>%
arrange(time)
combined
#> # A tibble: 75 x 3
#> time reading sensor
#> <dttm> <dbl> <dbl>
#> 1 1899-12-31 12:16:28 8 2
#> 2 1899-12-31 12:16:31 8 2
#> 3 1899-12-31 12:16:33 8 2
#> 4 1899-12-31 12:16:35 8 2
#> 5 1899-12-31 12:16:37 8 2
#> 6 1899-12-31 12:16:58 6 2
#> 7 1899-12-31 12:16:59 6 2
#> 8 1899-12-31 12:17:04 8 2
#> 9 1899-12-31 12:17:06 8 2
#> 10 1899-12-31 12:17:08 8 2
#> # ... with 65 more rows
像这样使用长格式的数据可以更轻松地绘图,例如:
library(ggplot2)
ggplot(combined, aes(time, reading, color = factor(sensor))) +
geom_line(size = 1) +
theme_bw(base_size = 16) +
scale_color_brewer(palette = "Set1", name = "Sensor")