What is the most elegant way to split data and create seasonal boxes?

I want to create seasonal boxes for many different time series. Hope the code below clearly illustrates what I want to do.

Now my question is: how to do this in the most elegant way with as many lines of code as possible. I can create a new object for each month with the "subset" function, and then build it, but it doesn't seem very elegant. I tried using the "split" function, but I do not know how to proceed from there.

Please tell me if my question is not clearly worded or edited to make it clearer.

Any direct help or contact with other sites / messages is appreciated. Thank you for your time.

Here is the code:

## Create Data Time <- seq(as.Date("2003/8/6"), as.Date("2011/8/5"), by = "2 weeks") data <- rnorm(209, mean = 15, sd = 1) DF <- data.frame(Time = Time, Data = data) DF[,3] <- as.numeric(format(DF$Time, "%m")) colnames(DF)[3] <- "Month" ## Create subsets Jan <- subset(DF, Month == 1) Feb <- subset(DF, Month == 2) Mar <- subset(DF, Month == 3) Apr <- subset(DF, Month == 4) ## Create boxplot months <- c("Jan", "Feb", "Mar", "Apr") boxplot(Jan$Data, Feb$Data, Mar$Data, Apr$Data, ylab = "Data", xlab = "Months", names = months) ## Try with "split" function DF.split <- split(DF, DF$Month) head(DF.split) 
+4
source share
3 answers

You better choose the monthly names directly in the "%b" format and use the ordered coefficient and formula interface for boxplot :

 DF$month <- factor(strftime(DF$Time,"%b"),levels=month.abb) boxplot(Data~month,DF) 

enter image description here

+4
source

Using 'ggplot2' (and the names of the month @James, thanks!):

 DF$month <- factor(strftime(DF$Time,"%b"),levels=month.abb) ggplot(DF, aes(x=,month, y=Data)) + geom_boxplot() 

boxplot

(By the way: note that in "ggplot2" the "Upper and lower" hinges "correspond to the first and third quartiles (the 25th and 7th percentiles). This is slightly different from the method used by the boxplot function, and may be with small samples ". - see documentation )

+5
source

To set the months as an ordered coefficient in any language setting , use the trick that can be found on the help page for ?month.abb :

 Sys.setlocale("LC_TIME", "German_Germany") DF$month <- factor(format(DF$Time, "%b"), levels=format(ISOdate(2000, 1:12, 1), "%b")) 

And you can also build it in lattice :

 require(lattice) bwplot(Data~month, DF, pch="|") # set pch to nice line instead of point 

Lattice boxplot

0
source

All Articles