R - Stock market data from csv to xts

后端 未结 1 611
無奈伤痛
無奈伤痛 2021-01-03 16:19

I have this data in a CSV:

Date  ALICORC1 ALT   ATACOBC1 AUSTRAC1 CONTINC1 BVN   DNT
40886 5.8      0.1   0.9      0.28     5.45     38.2  1.11
40889 5.8             


        
相关标签:
1条回答
  • 2021-01-03 16:36

    csv stands for comma-separated-values so the layout shown in the question is not csv. We will assume that the data really is in csv form and not in the form shown the question. If it truly is in the form shown in the question rather than csv then omit the sep="," argument in read.zoo below. Also if there are other deviations you may need to modify the arguments further. See ?read.zoo and the Reading Data in Zoo vignette in the zoo package.

    Here we use read.zoo in the zoo package to read in the data as a zoo object, z, and then we convert it to xts, x.

    See R News 4/1 which specifically treats date handling of Excel dates noting that we may need to modify the code below slightly if the Mac version of Excel is being used (as described there in the reference).

    library(xts) # this also loads zoo which has read.zoo
    
    toDate <- function(x) as.Date(x, origin = "1899-12-30")
    z <- read.zoo("myfile.csv", header = TRUE, sep = ",", FUN = toDate)
    x <- as.xts(z)
    
    0 讨论(0)
提交回复
热议问题