1

I have an xts object that includes multiple parameters over a 24 hour period (measurements each minute). Based on the time, I have added a column grouping into 4 'time of day' (tod) options: 'morning', 'afternoon', 'evening' and 'night'.

I would like to extract the mean values and standard deviations of the columns (parameters) for the entire period and also by time of day ('tod').

I have tried to first convert the xts object to a data frame, but have problems with the columns being of class factor instead of numeric. I have also tried 'aggregate' but am getting really strange outputs (or errors) when I use aggregate. Here is an example:

eg code to create a much smaller version of my data:

# time vector:
Time <- ISOdatetime(2015,01,01,6,12,0) + seq(0:(0.5*60-1))*1*60

# sample parameter columns
a <- 1:30
b <- 31:60
c<-seq(1,90,3)

# a sample xts object 'tester'
tester <- xts(cbind(a,b,c),Time)

# assign 'time of day':
tester$tod <- NA
tester$tod["T06:00/T06:20"]<-"night"
tester$tod["T06:21/T11:30"]<-"morning"
tester$tod["T06:31/T06:50"]<-"afternoon"

eg of how I have tried to get the mean values for a, b, c both for all data and by 'tod' using 'aggregate' (note that there are NA's in my data but this is not the issue):

tester$group = 1 #create a group column just to get the means for all data
mean_all <- aggregate(.~group, data=tester, FUN=mean, na.rm = TRUE, na.action=NULL)
meann_tod <- aggregate(.~tod, data=tester, FUN=mean, na.rm = TRUE, na.action=NULL)

Unfortunately this does not work, although there are no errors, the values are completely wrong.

Any advice would be much appreciated, I imagined this would be a very simple task!

1 Answers1

2

When you attempted creating a character vector, tod you would have needed to coerce the coredata matrix to be character rather than numeric. The authors did issue a warning when it basically refused to let you mess up your other data, but you ignored it (and I didn't understand it until I did some extra work.) You could construct a numeric vector to do the grouping:

> tester$tod <- NA
> tester$tod["T06:00/T06:20"]<-1
> tester$tod["T06:21/T11:30"]<-2
> tester$tod["T06:31/T06:50"]<-3
> 
> tester$group = 1 
> (mean_all <- aggregate(.~group, data=tester, FUN=mean, na.rm = TRUE, na.action=NULL))
  group    a    b    c      tod
1     1 15.5 45.5 44.5 2.133333
> (meann_tod <- aggregate(.~tod, data=tester, FUN=mean, na.rm = TRUE, na.action=NULL))
  tod    a    b    c group
1   1  4.5 34.5 11.5     1
2   2 13.5 43.5 38.5     1
3   3 24.5 54.5 71.5     1

I probably would have omitted the "group" variable from the formula:

> (meann_tod <- aggregate(cbind(a,b,c)~tod, data=tester, FUN=mean, na.rm = TRUE, na.action=NULL))
  tod    a    b    c
1   1  4.5 34.5 11.5
2   2 13.5 43.5 38.5
3   3 24.5 54.5 71.5
IRTFM
  • 258,963
  • 21
  • 364
  • 487