简体   繁体   中英

Why does my numeric variable have dates and how do I convert the whole thing to xts?

The head of my output looks like this; when I do str(output), it seems like it is a numeric of dimension 1, and I can't seem to extract the dates and obviously xts(output) doesn't work without the dates.

         2018-01-02 2018-01-03 2018-01-04 2018-01-05 2018-01-08 2018-01-09 2018-01-10 
 0.000   6511.339   6575.760   6584.575   6653.247   6676.615   6677.939   6662.670   

It seems that you have a named vector:

# create the vector `vector` with your values
vector <- c(6511.339, 6575.760, 6584.575, 6653.247, 6676.615, 6677.939, 6662.670)

# check vector (not named now)
vector

# add names to vector
names(vector) <- c("2018-01-02", "2018-01-03", "2018-01-04", "2018-01-05",
                  "2018-01-08"," 2018-01-09", "2018-01-10")

# now check named vector
vector

# get the names of the vector
names(vector)

#output:
[1] "2018-01-02"  "2018-01-03"  "2018-01-04"  "2018-01-05"  "2018-01-08"  " 2018-01-09"
[7] "2018-01-10" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM