data.frame到xts的转换结果带有引号

时间:2018-01-11 09:46:09

标签: xts quantmod

我知道stackoverflow上有各种各样的例子来讨论这个问题,但即使经过它们,我仍然无法找到解决方案。对于出现为字符的OHLCV,转换为数字的最佳方法是什么?我在这里错过了什么或做错了什么?

rm(list = ls(all = TRUE))
setwd("~/Documents/R")
library(jsonlite)
library(ggplot2)
library(forecast)
library(xts)
library(quantmod)
library(anytime)
library(highfrequency)
library(lubridate)


setwd("~/Documents/R/STK/csv")

gdax <- read.csv(file="GDAX_BTCUSD.csv", sep="\t", dec=".", header=FALSE,    stringsAsFactors = FALSE, na.strings = "NA")
options(scipen=999)
gdax <- na.omit(gdax[,c(9,4,5,6,7,8)])
colnames(gdax) <- c("Date", "Open", "High", "Low", "Close", "Volume")

> class(gdax)
[1] "data.frame"

> head(gdax)
                 Date           Open           High            Low          Close    Volume
1 2016-05-16 00:00:00 461.4800000000 461.4900000000 461.4700000000 461.4900000000  5.345398
2 2016-05-16 00:01:00 461.4700000000 461.4900000000 461.4700000000 461.4900000000  4.650470
3 2016-05-16 00:02:00 461.4800000000 461.4900000000 461.4700000000 461.4900000000  1.360030
4 2016-05-16 00:03:00 461.4900000000 461.4900000000 461.4900000000 461.4900000000  0.586860
5 2016-05-16 00:04:00 461.4700000000 461.4700000000 461.4700000000 461.4700000000  0.135180
6 2016-05-16 00:05:00 461.4700000000 461.5000000000 461.4400000000 461.5000000000 11.257970

GDAX_BTC <- xts(gdax[, 2:6], order.by=as.POSIXct(gdax[,1], tz="UTC"))

> head(GDAX_BTC)
                    Open             High             Low              Close            Volume         
2016-05-16 00:00:00 "461.4800000000" "461.4900000000" "461.4700000000" "461.4900000000" "   5.34539836"
2016-05-16 00:01:00 "461.4700000000" "461.4900000000" "461.4700000000" "461.4900000000" "   4.65047000"
2016-05-16 00:02:00 "461.4800000000" "461.4900000000" "461.4700000000" "461.4900000000" "   1.36003000"
2016-05-16 00:03:00 "461.4900000000" "461.4900000000" "461.4900000000" "461.4900000000" "   0.58686000"
2016-05-16 00:04:00 "461.4700000000" "461.4700000000" "461.4700000000" "461.4700000000" "   0.13518000"
2016-05-16 00:05:00 "461.4700000000" "461.5000000000" "461.4400000000" "461.5000000000" "  11.25797000"

> class(GDAX_BTC)
[1] "xts" "zoo"

GDAX_BTC <- xts(gdax[,-1], order.by=as.POSIXct(gdax$Date, format = "%Y-%m-%d %H:%M:%S"))
# no changes in the output.

R版本3.4.3(2017-11-30) 平台:x86_64-apple-darwin15.6.0(64位) 运行于:macOS High Sierra 10.13.2

Matrix产品:默认 BLAS:/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib LAPACK:/Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

区域设置: [1] en_GB.UTF-8 / en_GB.UTF-8 / en_GB.UTF-8 / C / en_GB.UTF-8 / en_GB.UTF-8

附加基础包: [1] stats graphics grDevices utils数据集方法库

其他附件包:  [1] lubridate_1.7.1 highfrequency_0.5.2 anytime_0.3.0 quantmod_0.4-12 TTR_0.23-2 xts_0.10-1
 [7] zoo_1.8-0 forecast_8.2 ggplot2_2.2.1 jsonlite_1.5

通过命名空间加载(而不是附加):  [1] Rcpp_0.12.14 magrittr_1.5 munsell_0.4.3 colorspace_1.3-2 lattice_0.20-35 rlang_0.1.6 quadprog_1.5-5
 [8] stringr_1.2.0 plyr_1.8.4 tools_3.4.3 nnet_7.3-12 parallel_3.4.3 grid_3.4.3 RApiDatetime_0.0.3 [15] timeDate_3042.101 gtable_0.2.0 tseries_0.10-42 yaml_2.1.16 lazyeval_0.2.1 lmtest_0.9-35 tibble_1.4.1
[22] curl_3.1 fracdiff_1.4-2 stringi_1.1.6 compiler_3.4.3 pillar_1.0.1 scales_0.5.0

0 个答案:

没有答案