我希望下面的问题不是太微不足道。我对 R 有点陌生,所以我非常感谢帮助和建议! 我有一个带有 ot tibbles 形式的 data.frames 的 data.list 。以前我自己使用 data.frames。 xlxs 格式的 data.frames。 这是 data.frime 的结构,在我调整名为“Time”的列之前:
structure(list(Time = structure(c(-2209074960, -2209074900, -2209074840,
-2209074780, -2209074720, -2209074660, -2209074600, -2209074540,
-2209074480, -2209074420, -2209074360, -2209074300, -2209074240,
-2209074180, -2209074120, -2209074060, -2209074000, -2209073940,
-2209073880, -2209073820), tzone = "UTC", class = c("POSIXct",
"POSIXt")), Kcal = c(1.899, 2.859, 2.519, 2.89, 2.89, 2.88, 3.68,
3.73, 3.07, 4.579, 4.889, 5.449, 9.51, 13.56, 13.63, 14.109,
14.289, 14.38, 14.84, 0.509), MET = c(2.5, 3.3, 3.3, 4.2, 4.6,
4.5, 6.2, 6.9, 7.4, 8.1, 9.5, 10.5, 11.6, 12.2, 12.8, 13.4, 13.8,
14.2, 14.3, 14.5), `MET in Kcal` = c(3.4125, 4.5045, 4.5045,
5.733, 6.279, 6.1425, 8.463, 9.4185, 10.101, 11.0565, 12.9675,
14.3325, 15.834, 16.653, 17.472, 18.291, 18.837, 19.383, 19.5195,
19.7925)), row.names = c(NA, -20L), class = c("tbl_df", "tbl",
"data.frame"))
data.frame 中的一列名为“Time”。值“Time”作为字符导入。为了调整时间,我将以下代码应用到我的 data.list:
for(i in seq_along(ErgoWithings_list))
{ErgoWithings_list[[i]]$Time <- gsub("1899-12-31 ","",as.character(ErgoWithings_list[[i]]$Time))}
到目前为止效果很好。 当我单独使用 data.frame 时,我执行了以下操作,将“时间”设置为时间值:
MyDateTime <- df$Time
MyDateTime <- strptime(MyDateTime, format = "%H:%M:%OS")
MyDateTime <- as.POSIXct(as.numeric(as.POSIXct(MyDateTime)) %% 86400, origin = "2021-09-28")
df$Time <- MyDateTime
这对我来说效果很好。我现在尝试将其应用到整个列表:
for(i in seq_along(ErgoWithings_list))
{MyDateTime <- ErgoWithings_list[[i]]$Time
MyDateTime <- strptime(MyDateTime, format = "%H:%M:%OS")
MyDateTime <- as.POSIXct(as.numeric(as.POSIXct(MyDateTime)) %% 86400, origin = "2021-09-28")
ErgoWithings_list[[i]]$Time <- MyDateTime}
但是,这不起作用,因为我收到通知,“}”是意外的。
如果我这样尝试:
for(i in seq_along(ErgoWithings_list))
MyDateTime <- ErgoWithings_list[[i]]$Time
MyDateTime <- strptime(MyDateTime, format = "%H:%M:%OS")
MyDateTime <- as.POSIXct(as.numeric(as.POSIXct(MyDateTime)) %% 86400, origin = "2021-09-28")
ErgoWithings_list[[i]]$Time <- MyDateTime
只有列表中的最后一个 data.frame 发生了某种变化,但之前的数据帧都没有发生变化。 有人可以帮我调整时间吗?此外,理想情况下,我只需要 data.frames/tibbles 中的给定时间,而无需日期(我通过“origin =...”设置)。但我不知道如何解决它。 谢谢!
library(lubridate)
library(dplyr)
library(purrr)
df %>%
mutate(Time = as.character(Time)) %>%
mutate(id = rep(row_number(), each=4, length.out=n())) %>%
group_split(id) %>%
map(~ .x %>%
separate(Time, into = c('date', 'time'), sep=' ', remove = FALSE) %>%
mutate(time = hms(time),
date = ymd(date)))
[[1]]
# A tibble: 4 x 7
Time date time Kcal MET `MET in Kcal` id
<chr> <date> <Period> <dbl> <dbl> <dbl> <int>
1 1899-12-31 00:04:00 1899-12-31 4M 0S 1.90 2.5 3.41 1
2 1899-12-31 00:05:00 1899-12-31 5M 0S 2.86 3.3 4.50 1
3 1899-12-31 00:06:00 1899-12-31 6M 0S 2.52 3.3 4.50 1
4 1899-12-31 00:07:00 1899-12-31 7M 0S 2.89 4.2 5.73 1
[[2]]
# A tibble: 4 x 7
Time date time Kcal MET `MET in Kcal` id
<chr> <date> <Period> <dbl> <dbl> <dbl> <int>
1 1899-12-31 00:08:00 1899-12-31 8M 0S 2.89 4.6 6.28 2
2 1899-12-31 00:09:00 1899-12-31 9M 0S 2.88 4.5 6.14 2
3 1899-12-31 00:10:00 1899-12-31 10M 0S 3.68 6.2 8.46 2
4 1899-12-31 00:11:00 1899-12-31 11M 0S 3.73 6.9 9.42 2
[[3]]
# A tibble: 4 x 7
Time date time Kcal MET `MET in Kcal` id
<chr> <date> <Period> <dbl> <dbl> <dbl> <int>
1 1899-12-31 00:12:00 1899-12-31 12M 0S 3.07 7.4 10.1 3
2 1899-12-31 00:13:00 1899-12-31 13M 0S 4.58 8.1 11.1 3
3 1899-12-31 00:14:00 1899-12-31 14M 0S 4.89 9.5 13.0 3
4 1899-12-31 00:15:00 1899-12-31 15M 0S 5.45 10.5 14.3 3
[[4]]
# A tibble: 4 x 7
Time date time Kcal MET `MET in Kcal` id
<chr> <date> <Period> <dbl> <dbl> <dbl> <int>
1 1899-12-31 00:16:00 1899-12-31 16M 0S 9.51 11.6 15.8 4
2 1899-12-31 00:17:00 1899-12-31 17M 0S 13.6 12.2 16.7 4
3 1899-12-31 00:18:00 1899-12-31 18M 0S 13.6 12.8 17.5 4
4 1899-12-31 00:19:00 1899-12-31 19M 0S 14.1 13.4 18.3 4
[[5]]
# A tibble: 4 x 7
Time date time Kcal MET `MET in Kcal` id
<chr> <date> <Period> <dbl> <dbl> <dbl> <int>
1 1899-12-31 00:20:00 1899-12-31 20M 0S 14.3 13.8 18.8 5
2 1899-12-31 00:21:00 1899-12-31 21M 0S 14.4 14.2 19.4 5
3 1899-12-31 00:22:00 1899-12-31 22M 0S 14.8 14.3 19.5 5
4 1899-12-31 00:23:00 1899-12-31 23M 0S 0.509 14.5 19.8 5