简体   繁体   中英

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! I have a data.list with data.frames in the form ot tibbles. Previously I worked with the data.frames on their own. The data.frames that are in xlxs format. This is the structure of a data.frime, before I adjust the column named '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"))

One of the columns in the data.frame is named 'Time'. The value 'Time' is imported as a character. In order to adjust the Time I apply following code to my 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:

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. 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 =...'). But I don't know how to work around it. Thanks!

  1. First we create a list of dataframes out of your provided data and set Time to character: row 1-3.
  2. then we iterate through all dataframes with map and change the class of Time to date and period after separating to each
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM