I have a dataframe called df where all columns are of class "POSIXct". Like that:
df
X1 X2
1 2000-01-01 00:00:00 2000-01-01 00:00:03
2 2000-01-01 00:00:01 2000-01-01 00:00:04
3 2000-01-01 00:00:02 2000-01-01 00:00:05
I want to have all values as one "POSIXct" vector by extracting the values rowwise. So this is my expected output:
vec <- magicfunction(df)
vec
2000-01-01 00:00:00 2000-01-01 00:00:03 2000-01-01 00:00:01 2000-01-01 00:00:04 2000-01-01 00:00:02 2000-01-01 00:00:05
Unfortunately, do.call("c", df) works columnwise. There is a question about how to unlist rowwise where it is recommended to use t() but using the t() function converts the dataframe to a matrix which also does not work with Dates. Further, apply(df, 1, function(x) ...) processes dataframes rowwise, but apply() also changes dates. On the other hand, lapply() works with Dates but it works columnwise.
This is the data I used in the example:
library(lubridate)
k <- 2
vec <- seq(ymd_hms("2000-01-01 00:00:00"),
ymd_hms("2000-01-01 00:00:5"),
by = 1)
df <- as.data.frame(split(vec, f = as.factor(rep(1:k, each = length(vec)/k))))