[英]How to convert a column to a 'Time' value for all tibbles in a list?
I hope the following problem is not too trivial.我希望下面的问题不是太琐碎。 I am kind of new to R, so I really appreciate help and advice!我是 R 的新手,所以我非常感谢帮助和建议! I have a data.list with data.frames in the form ot tibbles.我有一个带有 ot tibbles 形式的 data.frames 的 data.list。 Previously I worked with the data.frames on their own.以前我自己使用 data.frames。 The data.frames that are in xlxs format. xlxs 格式的 data.frames。 This is the structure of a data.frime, before I adjust the column named 'Time':这是 data.frime 的结构,在我调整名为“时间”的列之前:
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"))
One of the columns in the data.frame is named 'Time'. data.frame 中的一列名为“时间”。 The value 'Time' is imported as a character.值“时间”作为字符导入。 In order to adjust the Time I apply following code to my data.list:为了调整时间,我将以下代码应用于我的 data.list:
for(i in seq_along(ErgoWithings_list))
{ErgoWithings_list[[i]]$Time <- gsub("1899-12-31 ","",as.character(ErgoWithings_list[[i]]$Time))}
This works fine so far.到目前为止,这工作正常。 As I worked with the data.frame on its own I did following operation, to set 'Time' as a time value:当我自己使用 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
This worked fine for me.这对我来说很好。 I tried to apply it now to the whole list:我现在尝试将其应用于整个列表:
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}
However, this does not work, as I get the notice, that '}' is unexpected.但是,这不起作用,因为我收到通知,'}' 是意外的。
If I try it like that:如果我这样尝试:
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
only the last data.frame in the list changes somehow, but none of the ones before.只有列表中的最后一个 data.frame 以某种方式发生了变化,但之前没有任何变化。 Could someone help me out with adjusting the time?有人可以帮我调整时间吗? Additionally, I would ideally want only the given time in the data.frames/tibbles without having also a date (which I set by 'origin =...').此外,理想情况下,我只想要 data.frames/tibbles 中的给定时间,而没有日期(我通过“origin =...”设置)。 But I don't know how to work around it.但我不知道如何解决它。 Thanks!谢谢!
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.