无法在R中写入数字数据(包事件研究)
我在举一些例子。()这是我的firmData,而不是从Yahoo获取数据无法在R中写入数字数据(包事件研究),r,numeric,R,Numeric,我在举一些例子。()这是我的firmData,而不是从Yahoo获取数据 code date value KS233740Equity 05.12.2016 7225 KS233740Equity 07.12.2016 7235 KS233740Equity 06.12.2016 7355 KS233740Equity 08.12.2016 7395 KS233740Equity 02.12.20
code date value
KS233740Equity 05.12.2016 7225
KS233740Equity 07.12.2016 7235
KS233740Equity 06.12.2016 7355
KS233740Equity 08.12.2016 7395
KS233740Equity 02.12.2016 7430
KS233160Equity 05.12.2016 7540
KS233160Equity 07.12.2016 7605
KS233740Equity 09.12.2016 7650
KS233740Equity 01.12.2016 7665
KS233740Equity 28.11.2016 7680
KS233160Equity 06.12.2016 7705
KS233740Equity 30.11.2016 7720
KS233740Equity 24.11.2016 7750
KS233740Equity 29.11.2016 7760
KS233160Equity 08.12.2016 7770
KS233160Equity 02.12.2016 7800
KS233740Equity 25.11.2016 7830
KS233740Equity 12.12.2016 7850
KS233740Equity 25.01.2017 7855
KS233740Equity 24.01.2017 7885
导入时,为.numeric(firmData$value)=true
。但当我想以特定的方式将其传输到csv文件时,这似乎变成了非数字
library(dplyr)
firmData %>%
dplyr::select(code, date, value) %>%
dplyr::mutate(date = format(date, "%d.%m.%Y")) # this is for this specific code
readr::write_delim(path = "02_firmDataPrice.csv",
delim = ";", col_names = F)
library(EventStudy)
key <- "573e58c665fcc08cc6e5a660beaad0cb"
est <- EventStudyAPI$new()
est$authentication(apiKey = key)
esaParams <- EventStudy::ARCApplicationInput$new()
esaParams$setResultFileType("csv")
esaParams$setBenchmarkModel("garch")
dataFiles <- c("request_file" = "01_requestFile.csv",
"firm_data" = "02_firmDataPrice.csv",
"market_data" = "03_marketDataPrice.csv")
EventStudy::checkFiles(dataFiles)
我最终使用了他们提供的Eviews加载项,如果您需要做的只是将数字从class
字符转换为class数字,请尝试以下代码
首先,您有字符,因为您的数据集请求
是类矩阵
的对象,并且在R矩阵中只能保存一种类型的数据。因此,将矩阵强制为data.frame
。然后lappy
将函数as.numeric
添加到相应的列中
request <- as.data.frame(request, stringsAsFactors = FALSE)
request[6:9] <- lapply(request[6:9], as.numeric)
request
# V1 V2 V3 V4 group V6 V7 V8 V9
#1 1 KS233740Equity indexData 04.04.2018 leverage -10 10 -11 250
#2 2 KS233160Equity indexData 04.04.2018 leverage -10 10 -11 250
#3 3 KS278240Equity indexData 04.04.2018 Other -10 10 -11 250
您能否在问题中发布dput(head(request,10))
和dput(head(market_data,10))
的输出?此外,请参见@Rui Barradas,dput(head(request,5))=结构(c(“1”、“2”、“3”、“KS233740Equity”、“KS233160Equity”、“KS278240 equity”、“indexData”、“indexData”、“indexData”、“indexData”、“04.04.2018”、“04.04.2018”、“04.04.2018”、“杠杆”、“杠杆”、“其他”、“10”、“10”、“10”、“11”、“11”、“11”、“250”、“250”)、.Dim=c(3L,9L),.Dimnames=list(NULL,c(“,”,“,”,“,”组“,”组“,”组“,”组“,”组“,”组“,”组“,”组“,”数据头(市场数据,5))=structure(list)(value=c(889.2882.36895.21903.64903.01),code=structure(c(1L,1L,1L,1L),.Label=“KOSDAQ150”,class=“factor”),date=structure(c(1357084800,1357171200,1357257600,1357516800,1357603200),class=c(“POSIXct”,“POSIXt”),tzone=“UTC”),.Names=c(“value”,“code”,“date”),row.Names=c(“2013-01-02”,“2013-01-03”,“2013-01-04”,“2013-01-07”,“2013-01-08”),class=“data.frame”)
[链接]
request <- as.data.frame(request, stringsAsFactors = FALSE)
request[6:9] <- lapply(request[6:9], as.numeric)
request
# V1 V2 V3 V4 group V6 V7 V8 V9
#1 1 KS233740Equity indexData 04.04.2018 leverage -10 10 -11 250
#2 2 KS233160Equity indexData 04.04.2018 leverage -10 10 -11 250
#3 3 KS278240Equity indexData 04.04.2018 Other -10 10 -11 250
str(request)
#'data.frame': 3 obs. of 9 variables:
# $ V1 : chr "1" "2" "3"
# $ V2 : chr "KS233740Equity" "KS233160Equity" "KS278240Equity"
# $ V3 : chr "indexData" "indexData" "indexData"
# $ V4 : chr "04.04.2018" "04.04.2018" "04.04.2018"
# $ group: chr "leverage" "leverage" "Other"
# $ V6 : num -10 -10 -10
# $ V7 : num 10 10 10
# $ V8 : num -11 -11 -11
# $ V9 : num 250 250 250