r - combine 2 columns into 1 with special order

时间:2017-04-10 00:28:58

标签: r

I couldn't find an answer to this potential issue as I don't really know how to describe what I would like to achieve in a few words. Basically I have 2 columns (sunrise and sunset) with a certain number of rows. I wish to combine them into one column so that the first value of the combined column takes the value in the first row and first column, the second value in the combined column the value in first row and second column, the value in third row of combined column the value in second row and first column etc. WIth data we start with this:

df <- structure(list(sunrise = structure(c(1439635810.57809, 1439722237.7463, 
1439808664.71935, 1439895091.49609, 1439981518.07612, 1440067944.45978
), class = c("POSIXct", "POSIXt")), sunset = structure(c(1439682771.28069, 
1439769119.75559, 1439855467.39929, 1439941814.23447, 1440028160.28404, 
1440114505.57116), class = c("POSIXct", "POSIXt"))), .Names = c("sunrise", 
"sunset"), row.names = c(NA, 6L), class = "data.frame")

             sunrise              sunset
1 2015-08-15 06:50:10 2015-08-15 19:52:51
2 2015-08-16 06:50:37 2015-08-16 19:51:59
3 2015-08-17 06:51:04 2015-08-17 19:51:07
4 2015-08-18 06:51:31 2015-08-18 19:50:14
5 2015-08-19 06:51:58 2015-08-19 19:49:20
6 2015-08-20 06:52:24 2015-08-20 19:48:25

The desired outcome should look like:

data.frame(c("2015-08-15 06:50:10", "2015-08-15 19:52:51", "2015-08-16 06:50:37", 
"2015-08-16 19:51:59", "2015-08-17 06:51:04", "2015-08-17 19:51:07", 
"2015-08-18 06:51:31", "2015-08-18 19:50:14", "2015-08-19 06:51:58", 
"2015-08-19 19:49:20", "2015-08-20 06:52:24", "2015-08-20 19:48:25"
))

  output
1  2015-08-15 06:50:10
2  2015-08-15 19:52:51
3  2015-08-16 06:50:37
4  2015-08-16 19:51:59
5  2015-08-17 06:51:04
6  2015-08-17 19:51:07
7  2015-08-18 06:51:31
8  2015-08-18 19:50:14
9  2015-08-19 06:51:58
10 2015-08-19 19:49:20
11 2015-08-20 06:52:24
12 2015-08-20 19:48:25

I can then assign day/night to each row, and use these bins to categorize my data in day and night using the findInterval function.

Any help is greatly appreciated.

EDIT: Thanks for the answers, they work like a charm

2 个答案:

答案 0 :(得分:3)

Extract the rows iteratively and then convert into a vector

data.frame(output = as.POSIXct(Reduce(c, (apply(df, 1, c)))))
#                output
#1  2015-08-15 05:50:10
#2  2015-08-15 18:52:51
#3  2015-08-16 05:50:37
#4  2015-08-16 18:51:59
#5  2015-08-17 05:51:04
#6  2015-08-17 18:51:07
#7  2015-08-18 05:51:31
#8  2015-08-18 18:50:14
#9  2015-08-19 05:51:58
#10 2015-08-19 18:49:20
#11 2015-08-20 05:52:24
#12 2015-08-20 18:48:25

#NOTE: the values are different because of timezone

OR index the values from the data.frame directly

as.POSIXct(df[cbind(sort(rep(1:NROW(df), NCOL(df))), rep(1:NCOL(df), NROW(df)))])

答案 1 :(得分:2)

## create a matrix of indices then order it
o <- order(matrix(1:prod(dim(df)), nrow(df), byrow = TRUE))
## create the new data frame from the concatenated dates and the order vector
data.frame(output = do.call("c", c(df, use.names = FALSE))[o])
#                 output
# 1  2015-08-15 03:50:10
# 2  2015-08-15 16:52:51
# 3  2015-08-16 03:50:37
# 4  2015-08-16 16:51:59
# 5  2015-08-17 03:51:04
# 6  2015-08-17 16:51:07
# 7  2015-08-18 03:51:31
# 8  2015-08-18 16:50:14
# 9  2015-08-19 03:51:58
# 10 2015-08-19 16:49:20
# 11 2015-08-20 03:52:24
# 12 2015-08-20 16:48:25