0

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))))
  • 1
    You could try the (sadly cryptic) `do.call("c",df[1,])`. See [here](https://stackoverflow.com/q/15659783/324364) for some more discussion. – joran May 23 '19 at 14:27
  • @joran: thank you a lot. I actually lile do.call() but didnt think of using it here. If you could post it as an answer I will mark my question as answered. –  May 23 '19 at 14:53
  • Glad it solved your problem; easier to just point this question to the existing solution at the other question. – joran May 23 '19 at 14:55
  • @joran I worked on with my code and noticed that do.call("c",df) does not solve my problem because I actually need to unlist rowwise. Please consider to unmark my question as a duplicate so people can answer my question. –  May 27 '19 at 08:35
  • My comment specifically showed how to process one row. The only way to do what you want is to do that on each row individually and then combine them. Data frames are fundamentally organized column wise. – joran May 27 '19 at 21:18
  • Or I suppose you could use melt or gather to converrt it to a single column first. – joran May 27 '19 at 21:47
  • @joran:since it is unclear how to get the exepcted result and the link does not solve it, could you please unmark it s a duplicate? Maybe someone has an idea and can provide a peace of code. Thank you –  May 28 '19 at 04:32
  • I added an answer, but please note that what you tried, `do.call("c",df)` was not what was suggested to you. You have to do each row individually, like I said. – joran May 28 '19 at 04:45

1 Answers1

0

As I mentioned in my comment, you can use the answer in the duplicate repeatedly on each line:

df1 <- split(df,1:nrow(df))
do.call("c",lapply(df1,function(x) do.call("c",x)))

Note that neither the duplicate nor my comment suggests do.call("c",df) on the entire data frame, so it is not surprising that that does not work.

joran
  • 169,992
  • 32
  • 429
  • 468
  • If you use do.call("c",df) you will get a vector of Dates - just as I want. The problem is that the vector is created columnwise. That is what I meant. Thank you for this solution! –  May 28 '19 at 06:06