在 R 中按时间秒分组
group by time seconds in R
有谁知道如何按 'time' 秒分组?时间变量每秒有 30 个观测值。
structure(list(time = structure(c(1475049600, 1475049600.03333,
1475049600.06667, 1475049600.1, 1475049600.13333, 1475049600.16667,
1475049600.2, 1475049600.23333, 1475049600.26667, 1475049600.3,
1475049600.33333, 1475049600.36667, 1475049600.4, 1475049600.43333,
1475049600.46667, 1475049600.5, 1475049600.53333, 1475049600.56667,
1475049600.6, 1475049600.63333, 1475049600.66667, 1475049600.7,
1475049600.73333, 1475049600.76667, 1475049600.8, 1475049600.83333,
1475049600.86667, 1475049600.9, 1475049600.93333, 1475049600.96667,
1475049601, 1475049601.03333, 1475049601.06667, 1475049601.1,
1475049601.13333, 1475049601.16667, 1475049601.2, 1475049601.23333,
1475049601.26667, 1475049601.3), class = c("POSIXct", "POSIXt"
), tzone = "GMT"), X = c(-0.059, -0.023, -0.016, -0.016, -0.016,
-0.012, -0.016, -0.012, -0.016, -0.012, -0.016, -0.016, -0.016,
-0.02, -0.02, -0.027, -0.016, -0.02, -0.016, -0.016, -0.02, -0.023,
-0.016, -0.02, -0.012, -0.016, -0.012, -0.016, -0.016, -0.02,
-0.02, -0.02, -0.02, -0.008, -0.02, -0.016, -0.016, -0.012, -0.016,
-0.016), Y = c(-0.777, -0.777, -0.777, -0.777, -0.777, -0.777,
-0.777, -0.781, -0.773, -0.777, -0.777, -0.777, -0.777, -0.773,
-0.777, -0.777, -0.777, -0.777, -0.773, -0.777, -0.777, -0.777,
-0.77, -0.777, -0.777, -0.777, -0.781, -0.777, -0.777, -0.773,
-0.777, -0.777, -0.777, -0.773, -0.777, -0.777, -0.777, -0.777,
-0.777, -0.777), Z = c(0.688, 0.688, 0.691, 0.691, 0.688, 0.688,
0.688, 0.691, 0.691, 0.695, 0.688, 0.691, 0.691, 0.695, 0.691,
0.688, 0.688, 0.688, 0.691, 0.691, 0.691, 0.691, 0.695, 0.691,
0.691, 0.688, 0.695, 0.688, 0.691, 0.691, 0.688, 0.688, 0.688,
0.688, 0.688, 0.691, 0.691, 0.695, 0.695, 0.691)), subject_name = "1", time_zone = "02:00:00", missingness = structure(list(
time = structure(c(1475747248, 1475747249, 1475747250, 1475747292,
1475747293, 1475747294), class = c("POSIXct", "POSIXt"), tzone = "GMT"),
n_missing = c(30L, 30L, 1230L, 30L, 30L, 32490L)), class = "data.frame", row.names = c("1475747248",
"1475747249", "1475747250", "1475747292", "1475747293", "1475747294"
)), old_version = FALSE, firmware = "1.5.0", last_sample_time = structure(1475748377, tzone = "GMT", class = c("POSIXct",
"POSIXt")), serial_prefix = "TAS", sample_rate = 30L, acceleration_min = "-8.0", acceleration_max = "8.0", header = structure(list(
`Serial Number` = "TAS1E44150325", `Device Type` = "Link",
Firmware = "1.5.0", `Battery Voltage` = "3,88", `Sample Rate` = 30,
`Start Date` = structure(1475049600, tzone = "GMT", class = c("POSIXct",
"POSIXt")), `Stop Date` = structure(1475859600, tzone = "GMT", class = c("POSIXct",
"POSIXt")), `Last Sample Time` = structure(1475748377, tzone = "GMT", class = c("POSIXct",
"POSIXt")), TimeZone = "02:00:00", `Download Date` = structure(1475748377, tzone = "GMT", class = c("POSIXct",
"POSIXt")), `Board Revision` = "5", `Unexpected Resets` = "0",
`Acceleration Scale` = 256L, `Acceleration Min` = "-8.0",
`Acceleration Max` = "8.0", Mass = "77,1107028999572", Age = "22",
Limb = "Waist", DateOfBirth = "628890912000000000", `Subject Name` = "1",
`Serial Prefix` = "TAS"), class = c("gt3x_info", "list")), start_time = structure(1475049600, tzone = "GMT", class = c("POSIXct",
"POSIXt")), stop_time = structure(1475859600, tzone = "GMT", class = c("POSIXct",
"POSIXt")), total_records = 20929470L, bad_samples = FALSE, n_head = 40, row.names = c(NA,
20929470L), class = c("activity_df", "activity_df", "activity_df",
"data.frame"))
一个简单的方法是使用 {lubridate}
包中的 second
函数。这是一个使用简单数据的示例:
example <- structure(list(time = structure(c(1569632400, 1569632400, 1569632400,
1569632401, 1569632401, 1569632401), class = c("POSIXct", "POSIXt"
), tzone = ""), X = c(0, 3, 3, 2, 2, 0), Y = c(1, 2, 1, 1, 1,
0), Z = c(1, 1, 1, 0, 0, 1)), class = "data.frame", row.names = c(NA,
-6L))
example
time X Y Z
1 2019-09-28 08:00:00 0 1 1
2 2019-09-28 08:00:00 3 2 1
3 2019-09-28 08:00:00 3 1 1
4 2019-09-28 08:00:01 2 1 0
5 2019-09-28 08:00:01 2 1 0
6 2019-09-28 08:00:01 0 0 1
library(lubridate)
library(dplyr)
example |> group_by(second(time)) |> summarise(mean(X), mean(Y), mean(Z))
# A tibble: 2 × 4
`second(time)` `mean(X)` `mean(Y)` `mean(Z)`
<dbl> <dbl> <dbl> <dbl>
1 0 2 1.33 1
2 1 1.33 0.667 0.333
有谁知道如何按 'time' 秒分组?时间变量每秒有 30 个观测值。
structure(list(time = structure(c(1475049600, 1475049600.03333,
1475049600.06667, 1475049600.1, 1475049600.13333, 1475049600.16667,
1475049600.2, 1475049600.23333, 1475049600.26667, 1475049600.3,
1475049600.33333, 1475049600.36667, 1475049600.4, 1475049600.43333,
1475049600.46667, 1475049600.5, 1475049600.53333, 1475049600.56667,
1475049600.6, 1475049600.63333, 1475049600.66667, 1475049600.7,
1475049600.73333, 1475049600.76667, 1475049600.8, 1475049600.83333,
1475049600.86667, 1475049600.9, 1475049600.93333, 1475049600.96667,
1475049601, 1475049601.03333, 1475049601.06667, 1475049601.1,
1475049601.13333, 1475049601.16667, 1475049601.2, 1475049601.23333,
1475049601.26667, 1475049601.3), class = c("POSIXct", "POSIXt"
), tzone = "GMT"), X = c(-0.059, -0.023, -0.016, -0.016, -0.016,
-0.012, -0.016, -0.012, -0.016, -0.012, -0.016, -0.016, -0.016,
-0.02, -0.02, -0.027, -0.016, -0.02, -0.016, -0.016, -0.02, -0.023,
-0.016, -0.02, -0.012, -0.016, -0.012, -0.016, -0.016, -0.02,
-0.02, -0.02, -0.02, -0.008, -0.02, -0.016, -0.016, -0.012, -0.016,
-0.016), Y = c(-0.777, -0.777, -0.777, -0.777, -0.777, -0.777,
-0.777, -0.781, -0.773, -0.777, -0.777, -0.777, -0.777, -0.773,
-0.777, -0.777, -0.777, -0.777, -0.773, -0.777, -0.777, -0.777,
-0.77, -0.777, -0.777, -0.777, -0.781, -0.777, -0.777, -0.773,
-0.777, -0.777, -0.777, -0.773, -0.777, -0.777, -0.777, -0.777,
-0.777, -0.777), Z = c(0.688, 0.688, 0.691, 0.691, 0.688, 0.688,
0.688, 0.691, 0.691, 0.695, 0.688, 0.691, 0.691, 0.695, 0.691,
0.688, 0.688, 0.688, 0.691, 0.691, 0.691, 0.691, 0.695, 0.691,
0.691, 0.688, 0.695, 0.688, 0.691, 0.691, 0.688, 0.688, 0.688,
0.688, 0.688, 0.691, 0.691, 0.695, 0.695, 0.691)), subject_name = "1", time_zone = "02:00:00", missingness = structure(list(
time = structure(c(1475747248, 1475747249, 1475747250, 1475747292,
1475747293, 1475747294), class = c("POSIXct", "POSIXt"), tzone = "GMT"),
n_missing = c(30L, 30L, 1230L, 30L, 30L, 32490L)), class = "data.frame", row.names = c("1475747248",
"1475747249", "1475747250", "1475747292", "1475747293", "1475747294"
)), old_version = FALSE, firmware = "1.5.0", last_sample_time = structure(1475748377, tzone = "GMT", class = c("POSIXct",
"POSIXt")), serial_prefix = "TAS", sample_rate = 30L, acceleration_min = "-8.0", acceleration_max = "8.0", header = structure(list(
`Serial Number` = "TAS1E44150325", `Device Type` = "Link",
Firmware = "1.5.0", `Battery Voltage` = "3,88", `Sample Rate` = 30,
`Start Date` = structure(1475049600, tzone = "GMT", class = c("POSIXct",
"POSIXt")), `Stop Date` = structure(1475859600, tzone = "GMT", class = c("POSIXct",
"POSIXt")), `Last Sample Time` = structure(1475748377, tzone = "GMT", class = c("POSIXct",
"POSIXt")), TimeZone = "02:00:00", `Download Date` = structure(1475748377, tzone = "GMT", class = c("POSIXct",
"POSIXt")), `Board Revision` = "5", `Unexpected Resets` = "0",
`Acceleration Scale` = 256L, `Acceleration Min` = "-8.0",
`Acceleration Max` = "8.0", Mass = "77,1107028999572", Age = "22",
Limb = "Waist", DateOfBirth = "628890912000000000", `Subject Name` = "1",
`Serial Prefix` = "TAS"), class = c("gt3x_info", "list")), start_time = structure(1475049600, tzone = "GMT", class = c("POSIXct",
"POSIXt")), stop_time = structure(1475859600, tzone = "GMT", class = c("POSIXct",
"POSIXt")), total_records = 20929470L, bad_samples = FALSE, n_head = 40, row.names = c(NA,
20929470L), class = c("activity_df", "activity_df", "activity_df",
"data.frame"))
一个简单的方法是使用 {lubridate}
包中的 second
函数。这是一个使用简单数据的示例:
example <- structure(list(time = structure(c(1569632400, 1569632400, 1569632400,
1569632401, 1569632401, 1569632401), class = c("POSIXct", "POSIXt"
), tzone = ""), X = c(0, 3, 3, 2, 2, 0), Y = c(1, 2, 1, 1, 1,
0), Z = c(1, 1, 1, 0, 0, 1)), class = "data.frame", row.names = c(NA,
-6L))
example
time X Y Z
1 2019-09-28 08:00:00 0 1 1
2 2019-09-28 08:00:00 3 2 1
3 2019-09-28 08:00:00 3 1 1
4 2019-09-28 08:00:01 2 1 0
5 2019-09-28 08:00:01 2 1 0
6 2019-09-28 08:00:01 0 0 1
library(lubridate)
library(dplyr)
example |> group_by(second(time)) |> summarise(mean(X), mean(Y), mean(Z))
# A tibble: 2 × 4
`second(time)` `mean(X)` `mean(Y)` `mean(Z)`
<dbl> <dbl> <dbl> <dbl>
1 0 2 1.33 1
2 1 1.33 0.667 0.333