Reputation: 245
I have a file that has been saved as a binary compressed file that I recall using the following command
rates.data = readRDS(paste("~/Documents/.../..../RatesData/",
"2013-01-14", ".rds", sep = ""))
Which gives me:
rates.data
# Date USSW1 USSW2 USSW3 USSW4 USSW5 USSW7 USSW10 USSW30
# 3271 2013-01-14 0.31 0.38 0.50 0.67 0.89 1.34 1.88 2.83
But, I want only the rate. I have tried the following:
rates.data[2:9]
# USSW1 USSW2 USSW3 USSW4 USSW5 USSW7 USSW10 USSW30
# 3271 0.31 0.38 0.50 0.67 0.89 1.34 1.88 2.83
rate.data[1,[2:9]] this does not work
This gives me only one element...
rates.data[1,2]
[1] "0.31"
...or as a numeric:
as.numeric(rates.data[1,2])
[1] 0.31
But, I still can't get the vector. How do I extract the vector?
str(rates.data) # returns the following - so its actually a data frame
'data.frame': 1 obs. of 9 variables:
$ Date : chr "2013-01-14"
$ USSW1 : chr "0.31"
$ USSW2 : chr "0.38"
$ USSW3 : chr "0.50"
$ USSW4 : chr "0.67"
$ USSW5 : chr "0.89"
$ USSW7 : chr "1.34"
$ USSW10: chr "1.88"
$ USSW30: chr "2.83"
Upvotes: 1
Views: 938
Reputation: 33940
rates.data[,2:9]
df[row-indices , col-indices]
is all you need, and you can omit one if you specify the other, so here we just give col-indices.
(If you omit the comma, like rates.data[2:9]
, this still gets treated as column-indices, but it's sloppy.)
Upvotes: 0
Reputation: 17189
As @nograpes suggested.
as.numeric(rates.data[, 2:9])
## [1] 0.31 0.38 0.50 0.67 0.89 1.34 1.88 2.83
Upvotes: 3