Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R – Number of observations per month in an xts object (weekday data)

I have several xts-objects containing weekday data from 2001-01-01 to 2021-12-31. Now I need to know the number of observations within each month from January 2001 to December 2021 in order to further analyse them. How can I get these? I’m rather new to R (and programming), so I assume there is a simple formula for this I am unaware of.

    structure(c(6.5156, 6.5, 6.4531, 6, 5.8594, 5.8281, 5.8438, 5.8281, 
5.8438, 5.8438, 5.8438, 5.7969), class = c("xts", "zoo"), .CLASS = "double", index = structure(c(978307200, 
978393600, 978480000, 978566400, 978652800, 978912000, 978998400, 
979084800, 979171200, 979257600, 979516800, 979603200), tzone = "UTC", tclass = "Date"), .Dim = c(12L, 
1L))

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

xts has all kinds of period.apply functions you can use. for monthly: apply.monthly

Based on your example if you want the sum / mean / n of observations:

# sum
apply.monthly(my_xts, sum)
              [,1]
2001-01-16 72.1564

# mean
apply.monthly(my_xts, mean)
               [,1]
2001-01-16 6.013033

# n of records 
# length works like sum or mean, 
# but this is an example of how to use an anonymous function.
apply.monthly(my_xts, function(x) length(x))
           [,1]
2001-01-16   12

xts always takes the last day of the period to show the information.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading