0

I have a list of periods that I would like to convert it to behave like a vector (eventually to add as a column in a data frame).

library(lubridate)

x <- list(ms("09:10"),  ms("09:02"), ms("1:10"))

# some_function(x)
# with output
ms(c("09:10", "09:02", "1:10"))

unlist and purrr::flatten don't work in this case since it loses it period properties.

lioumens
  • 110
  • 4

1 Answers1

1
d <- do.call("c", x)
class(d)
[1] "Period"
attr(,"package")
[1] "lubridate"

Or

 d <- data.frame(date = do.call("c", x))

 str(d)
'data.frame':   3 obs. of  1 variable:
 $ date:Formal class 'Period' [package "lubridate"] with 6 slots
  .. ..@ .Data : num  10 2 10
  .. ..@ year  : num  0 0 0
  .. ..@ month : num  0 0 0
  .. ..@ day   : num  0 0 0
  .. ..@ hour  : num  0 0 0
  .. ..@ minute: num  9 9 1

d
    date
1 9M 10S
2  9M 2S
3 1M 10S

See here: why does unlist() kill dates in R

desval
  • 2,345
  • 2
  • 16
  • 23