Refer to the time series object by column name

I have a test data frame object that has column names:

 > test abcde 1 -0.67 -0.02 -0.10 -0.22 -0.32 2 0.46 -1.51 -0.79 0.26 1.19 3 0.22 -0.18 -1.40 0.41 -0.32 4 -2.21 0.79 0.36 1.00 -0.51 5 -0.69 0.39 -0.76 -0.73 -0.43 

In this format, I can easily access the columns using the test$b notation. I can easily convert this to a time series object:

 test.ts <- ts(test, frequency=<value>, start=<value> 

However, if it is a ts object, is there an easy way to access columns (or rows) by name instead of column number? The test.ts object still has the column name information shown with colnames :

 > colnames(test.ts) [1] "a" "b" "c" "d" "e" 

However, test.ts$b does not work. Note that “easy” I mean, without writing something ugly, like test.ts[,which(colnames(test.ts)=="b"] , because it's not easy, it's ugly. Yes, I could write my own function to do this, but I was wondering if there is a built-in way to do this. Thanks!


According to the request:

 > dput(head(a)) structure(list(a = c(-0.67, 0.46, 0.22, -2.21, -0.69, -0.45), b = c(-0.02, -1.51, -0.18, 0.79, 0.39, -1.33), c = c(-0.1, -0.79, -1.4, 0.36, -0.76, 0.15), d = c(-0.22, 0.26, 0.41, 1, -0.73, -2.23), e = c(-0.32, 1.19, -0.32, -0.51, -0.43, -0.58)), .Names = c("a", "b", "c", "d", "e"), row.names = c(NA, 6L), class = "data.frame") 
+7
source share
1 answer

Use a different subset syntax:

 test.ts[, 'b'] #Time Series: #Start = 1 #End = 6 #Frequency = 1 #[1] -0.02 -1.51 -0.18 0.79 0.39 -1.33 
+7
source

All Articles