data.frame - R - Extracting summary statistics for all columns in an xts object either directly or first converting to data frame -
i have xts object includes multiple parameters on 24 hr period (measurements each minute). based on time, have added column grouping 4 'time of day' (tod) options: 'morning', 'afternoon', 'evening' , 'night'.
i extract mean values , standard deviations of columns (parameters) entire period , time of day ('tod').
i have tried first convert xts object info frame, have problems columns beingness of class factor instead of numeric. have tried 'aggregate' getting unusual outputs (or errors) when utilize aggregate. here example:
eg code create much smaller version of data:
# time vector: time <- isodatetime(2015,01,01,6,12,0) + seq(0:(0.5*60-1))*1*60 # sample parameter columns <- 1:30 b <- 31:60 c<-seq(1,90,3) # 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 have tried mean values a, b, c both info , 'tod' using 'aggregate' (note there na's in info not issue):
tester$group = 1 #create grouping column means info 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 not work, although there no errors, values wrong.
any advice much appreciated, imagined simple task!
when attempted creating character vector, tod
have needed coerce coredata matrix character rather numeric. authors did issue warning when refused allow mess other data, ignored (and didn't understand until did work.) build numeric vector 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)) grouping 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 b c grouping 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 have omitted "group" variable formula:
> (meann_tod <- aggregate(cbind(a,b,c)~tod, data=tester, fun=mean, na.rm = true, na.action=null)) tod 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
r data.frame aggregate xts
No comments:
Post a Comment