Pull nth Day of Month in XTS in R
Using the same example data from the question you've linked to, you can do some basic subsetting.
Here's the sample data:
library(xts)
data(sample_matrix)
x <- as.xts(sample_matrix)
Here's the subsetting:
x[format(index(x), "%d") == "10"]
# Open High Low Close
# 2007-01-10 49.91228 50.13053 49.91228 49.97246
# 2007-02-10 50.68923 50.72696 50.60707 50.69562
# 2007-03-10 49.79370 49.88984 49.70385 49.88698
# 2007-04-10 49.55704 49.78776 49.55704 49.76984
# 2007-05-10 48.83479 48.84549 48.38001 48.38001
# 2007-06-10 47.74899 47.74899 47.28685 47.28685
Is this what you were looking for?
Using %in%
would give you some more flexibility. For instance, if you wanted the tenth, eleventh, and twelfth days of each month, you could use x[format(index(x), "%d") %in% c("10", "11", "12")]
instead.
Update
If, as you have in your update, you want to extract the tenth data point, just use an anonymous function as follows:
do.call(rbind, lapply(split(x, "months"), function(x) x[10]))
# Open High Low Close
# 2007-01-11 49.88529 50.23910 49.88529 50.23910
# 2007-02-10 50.68923 50.72696 50.60707 50.69562
# 2007-03-10 49.79370 49.88984 49.70385 49.88698
# 2007-04-10 49.55704 49.78776 49.55704 49.76984
# 2007-05-10 48.83479 48.84549 48.38001 48.38001
# 2007-06-10 47.74899 47.74899 47.28685 47.28685
Note that the first row is the eleventh day of the month, because the data actually starts on January 2, 2007.
x[1, ]
# Open High Low Close
# 2007-01-02 50.03978 50.11778 49.95041 50.11778
R - Number of observations per month in an xts object (weekday data)
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.
R: 3rd Wedndesday of a specific month using XTS
Why not just
library(xts)
x = xts(1:3650, Sys.Date()+1:3650)
x[.indexwday(x) == 3 &
.indexmday(x) >= 15 &
.indexmday(x) <= 21
]
If first Wednesday is on 1st then third is on 15th.
If first Wednesday is on 7th then third is on 21st.
So anywhere between 15th and 21st.
find last day of month in a sequence of dates
Try:
which(ave(as.numeric(ds),format(ds,"%Y%m"),FUN=function(x) x==max(x))==1)
Related Topics
Why Is R Dplyr::Mutate Inconsistent with Custom Functions
Automatically Detect Date Columns When Reading a File into a Data.Frame
Rmarkdown::Render() in a Loop - Cannot Allocate Vector of Size
How to Simultaneously Apply Color/Shape/Size in a Scatter Plot Using Plotly
How to Show a Loading Screen When the Output Is Being Calculated in a Background Process
Cumulative Sum in a Window (Or Running Window Sum) Based on a Condition in R
Finding the Index of First Changes in the Elements of a Vector
Space Between Gpplot2 Horizontal Legend Elements
Split a Data Frame Column Containing a List into Multiple Columns Using Dplyr (Or Otherwise)
Flatten Nested Lists in a List
Split Data.Frame into Groups by Column Name
Add Columns to a Reactive Data Frame in Shiny and Update Them
How to Put a Complicated Equation into a R Formula
Use Object Names as List Names in R