I am running a function in the oce
package which generates a matrix. I transpose the matrix and then convert the matrix to a dataframe, but my POSIXct formatted date becomes a list within the date column. Here is my reproducible piece of code:
library(oce)
library(dplyr)
df <- data.frame(times = as.POSIXct(rep("2011-03-03 06:49:00", 4)),
Latitude = c(39,38,34,35),
Longitude = c(-75,-74,-73,-72),
ref = rep(T, 4))
vsunAngle = Vectorize(sunAngle)
Sun.Angle <- as.data.frame(t(with(df, vsunAngle(times, longitude = Longitude, latitude = Latitude, useRefraction = ref))))
# This is the transposition without converting to a dataframe. Not the correct time format:
t(with(df, vsunAngle(times, longitude = Longitude, latitude = Latitude, useRefraction = ref)))
How do I successfully convert this matrix to a dataframe without losing the date? I already tried extracting the column-list into a new column, but I had less luck doing this.
Thank you in advance!
Sun.Angle
is a data frame for which each column is a list so apply do.call("c", ...)
to each such list to create a vector and convert the resulting list back to data frame. The last line is optional and will affect which time zone is shown.
Sun.Angle |>
lapply(do.call, what = "c") |>
as.data.frame() |>
transform(time = .POSIXct(time))
giving
time azimuth altitude diameter distance declination rightAscension
1 2011-03-03 06:49:00 101.1500 3.050665 0.5378838 0.9912921 -6.849591 -16.08594
2 2011-03-03 06:49:00 101.7251 3.971291 0.5378838 0.9912921 -6.849591 -16.08594
3 2011-03-03 06:49:00 102.0130 5.537658 0.5378838 0.9912921 -6.849591 -16.08594
4 2011-03-03 06:49:00 102.6942 6.116021 0.5378838 0.9912921 -6.849591 -16.08594