I'm not sure why this is difficult or why I can't find the answer to this.
I have multiple 5-column xts objects:
xts1
xts2
xts3
I then have a vector that stores the names of those xts objects:
xtsnames <- c("xts1", "xts2", "xts3")
I want to access and compute the sum of the fourth column in each of those xts objects. When I do:
sum(xts1[4])
it works. But when I do:
sum(xtsnames[1][4])
it doesn't work. I understand why it doesn't work (because xtsnames is a vector of characters), but I couldn't figure out how to do this.
Eventually, it will be in a for
loop similar to this:
for (i in 1:length(xtsnames)) {
fourthcol[,i] <- sum(xtsnames[i][4])
}