当我使用xts软件包将数据帧转换为时间序列数据时,R中的数值和整数变量被更改为字符变量
我在Rstudio版本4.0.2上编写代码。我的数据包括股票的价格和数量 当我运行代码时,数值和整数变量被更改为字符变量 我不明白我为什么会遇到这个问题,以及如何修复这个代码?如有任何提示,我将不胜感激 我用以下方式编码:当我使用xts软件包将数据帧转换为时间序列数据时,R中的数值和整数变量被更改为字符变量,r,time-series,xts,R,Time Series,Xts,我在Rstudio版本4.0.2上编写代码。我的数据包括股票的价格和数量 当我运行代码时,数值和整数变量被更改为字符变量 我不明白我为什么会遇到这个问题,以及如何修复这个代码?如有任何提示,我将不胜感激 我用以下方式编码: > original.HPG <- read.csv("excel_hpg.csv", header = TRUE) > keep.variables.HPG <- original.HPG[,c(1,2,7:11)] >
> original.HPG <- read.csv("excel_hpg.csv", header = TRUE)
> keep.variables.HPG <- original.HPG[,c(1,2,7:11)]
> names(keep.variables.HPG) <- paste(c("HPG.Ticker", "HPG.Date", "HPG.Volume",
+ "HPG.Open", "HPG.High", "HPG.Low", "HPG.Close"))
> library(lubridate)
> edit.date.HPG <- ymd(keep.variables.HPG[,2])
> class(edit.date.HPG)
[1] "Date"
> data.HPG <- cbind(edit.date.HPG, keep.variables.HPG[,-2])
> data.HPG <- data.HPG[order(data.HPG$edit.date.HPG),]
> data.HPG[c(1:3, nrow(data.HPG)), ]
edit.date.HPG HPG.Ticker HPG.Volume HPG.Open HPG.High HPG.Low
3210 2007-11-15 HPG 1306330 130.00 130.00 109.0
3209 2007-11-16 HPG 248510 121.00 121.00 121.0
3208 2007-11-19 HPG 120480 115.00 115.00 115.0
1 2020-10-06 HPG 23609700 27.45 28.25 27.2
HPG.Close
3210 127.00
3209 121.00
3208 115.00
1 28.25
> str(data.HPG)
'data.frame': 3210 obs. of 7 variables:
$ edit.date.HPG: Date, format: "2007-11-15" "2007-11-16" ...
$ HPG.Ticker : chr "HPG" "HPG" "HPG" "HPG" ...
$ HPG.Volume : int 1306330 248510 120480 58710 728080 266040 106370 135430 262120 131020 ...
$ HPG.Open : num 130 121 115 110 105 114 111 108 108 106 ...
$ HPG.High : num 130 121 115 110 114 114 114 110 108 107 ...
$ HPG.Low : num 109 121 115 110 105 110 110 107 105 106 ...
$ HPG.Close : num 127 121 115 110 114 114 110 109 105 107 ...
> library(xts)
> data.HPG <- xts(data.HPG[,2:6], order.by = data.HPG[,1])
> data.HPG[c(1:3, nrow(data.HPG)), ]
HPG.Ticker HPG.Volume HPG.Open HPG.High HPG.Low
2007-11-15 "HPG" " 1306330" "130.00" "130.00" "109.00"
2007-11-16 "HPG" " 248510" "121.00" "121.00" "121.00"
2007-11-19 "HPG" " 120480" "115.00" "115.00" "115.00"
2020-10-06 "HPG" "23609700" " 27.45" " 28.25" " 27.20"
> str(data.HPG)
An ‘xts’ object on 2007-11-15/2020-10-06 containing:
Data: chr [1:3210, 1:5] "HPG" "HPG" "HPG" "HPG" "HPG" "HPG" "HPG" "HPG" ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "HPG.Ticker" "HPG.Volume" "HPG.Open" "HPG.High" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
NULL
>original.HPG keep.variables.HPG名称(keep.variables.HPG)库(lubridate)
>edit.date.HPG类(edit.date.HPG)
[1] “日期”
>data.HPG data.HPG data.HPG[c(1:3,nrow(data.HPG)),]
编辑.date.HPG HPG.Ticker HPG.Volume HPG.Open HPG.High HPG.Low
3210 2007-11-15 HPG 1306330 130.00 130.00 109.0
3209 2007-11-16 HPG 248510121.00121.00121.0
3208 2007-11-19 HPG 120480 115.00 115.00 115.0
1202-10-06 HPG 23609700 27.45 28.25 27.2
HPG.关闭
3210 127.00
3209 121.00
3208 115.00
1 28.25
>str(data.HPG)
“数据帧”:3210 obs。共有7个变量:
$edit.date.HPG:日期,格式:“2007-11-15”“2007-11-16”。。。
$HPG.股票代码:chr“HPG”“HPG”“HPG”“HPG”。。。
$HPG.交易量:int 1306330 248510 120480 58710 728080 266040 106370 135430 262120 131020。。。
$HPG.未清:编号130 121 115 110 105 114 111 108 106。。。
$HPG.高位:数字130 121 115 110 114 114 110 108 107。。。
$HPG.低:数字109 121 115 110 105 110 107 105 106。。。
$HPG.收盘价:127 121 115 110 114 110 109 105 107。。。
>图书馆(xts)
>data.HPG data.HPG[c(1:3,nrow(data.HPG)),]
HPG.Ticker HPG.Volume HPG.Open HPG.High HPG.Low
2007-11-15“HPG”“1306330”“130.00”“130.00”“109.00”
2007-11-16“HPG”“248510”“121.00”“121.00”“121.00”
2007-11-19“HPG”“120480”“115.00”“115.00”“115.00”
2020-10-06“HPG”“23609700”“27.45”“28.25”“27.20”
>str(data.HPG)
2007-11-15/2020-10-06上的“xts”对象包含:
数据:chr[1:3210,1:5]“HPG”“HPG”“HPG”“HPG”“HPG”“HPG”“HPG”“HPG”“HPG”。。。
-属性(*,“dimnames”)=2个列表
..$:空
..$:chr[1:5]“高油价股票行情”高油价成交量“高油价开盘”高油价。。。
按类别的对象编制索引:[Date]TZ:UTC
xts属性:
无效的
从数据帧中删除字符列。尝试:
library(xts)
data.HPG <- xts(data.HPG[,3:6], order.by = data.HPG[,1])
库(xts)
data.HPG感谢您的帮助。它起作用了。