-1

I have a large data set that I am trying to reformat. Unfortunately, I cannot aggregate daily data.

dataset1_updated<- structure(list(X = 1:5, Time = structure(c(1L, 1L, 2L, 3L, 3L), .Label = c("7/29/11 10:29", "7/29/11 10:30", "7/29/11 10:32"
), class = "factor"), O3 = c(32.032608222367, 32.032608222367, 
32.032608222367, 32.032608222367, 32.032608222367), SO2 = c(2.611, 
2.605, 2.744, 2.767, 2.778), NO = c(0.081, 0.081, 0.081, 0.081, 
0.081), NO2 = c(1.938, 1.912, 1.912, 1.896, 1.863), NOx = c(2.019, 
1.993, 1.993, 1.977, 1.944)), .Names = c("X", "Time", "O3", 
"SO2", "NO", "NO2", "NOx"), row.names = c(NA, 5L), class = "data.frame")

I convert the data set to xts object, and apply daily mean function, the results are "NA". Could you please tell me what is missing?

x <- as.xts(as.POSIXct(dataset1_updated$Time, format="%m/%d/%Y %H:%M"))
x_up<- apply.daily(x, colMeans)
write.csv(as.data.frame(as.matrix(x_up)), file="test")

thank you,

nil
  • 1
  • 1

1 Answers1

0

We need to change the xts statement as the as.xts is applying only on the datetime class and not on the entire dataset

xt1 <- xts(dataset1_updated[-(1:2)], order.by = as.POSIXct(dataset1_updated$Time,
                              format = "%m/%d/%y %H:%M"))
x_up <- apply.daily(xt1, colMeans)
x_up
#                          O3   SO2    NO    NO2    NOx
#2011-07-29 10:32:00 32.03261 2.701 0.081 1.9042 1.9852
akrun
  • 874,273
  • 37
  • 540
  • 662