无法在 R 中以数字形式写入数据(包 EventStudy)
Cannot write data in numbers in R(package EventStudy)
我正在学习一些示例。(https://www.eventstudytools.com/blog/dieselgate-example-data-preparation) 这是我的公司数据,而不是从 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.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
当我导入这个时,is.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)
然后,
Error: Column firm is not of type Integer! inherits from numeric not character.
如何正确使用包 "EventStudy"?
(market_data 看起来与 "firmData" 非常相似,制作“01_requestFile.csv”的数据 "request" 如下所示。似乎没有问题,因为还没有。)
"1" "KS233740Equity" "indexData" "04.04.2018" "leverage" "-10" "10" "-11" "250"
"2" "KS233160Equity" "indexData" "04.04.2018" "leverage" "-10" "10" "-11" "250"
"3" "KS278240Equity" "indexData" "04.04.2018" "Other" "-10" "10" "-11" "250"
我最终使用了他们提供的 Eviews 插件,https://github.com/EventStudyTools/api-wrapper.r/
如果您需要做的只是将数字从 class character
转换为 class numeric
,请尝试以下代码。
首先,你有字符,因为你的数据集 request
是 class matrix
的对象,并且在 R 矩阵中只能包含一种类型的数据。所以将矩阵强制转换为data.frame
。然后lapply
函数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
然后您可以使用 str
检查 data.frame
:
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
我正在学习一些示例。(https://www.eventstudytools.com/blog/dieselgate-example-data-preparation) 这是我的公司数据,而不是从 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.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
当我导入这个时,is.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)
然后,
Error: Column firm is not of type Integer! inherits from numeric not character.
如何正确使用包 "EventStudy"?
(market_data 看起来与 "firmData" 非常相似,制作“01_requestFile.csv”的数据 "request" 如下所示。似乎没有问题,因为还没有。)
"1" "KS233740Equity" "indexData" "04.04.2018" "leverage" "-10" "10" "-11" "250"
"2" "KS233160Equity" "indexData" "04.04.2018" "leverage" "-10" "10" "-11" "250"
"3" "KS278240Equity" "indexData" "04.04.2018" "Other" "-10" "10" "-11" "250"
我最终使用了他们提供的 Eviews 插件,https://github.com/EventStudyTools/api-wrapper.r/
如果您需要做的只是将数字从 class character
转换为 class numeric
,请尝试以下代码。
首先,你有字符,因为你的数据集 request
是 class matrix
的对象,并且在 R 矩阵中只能包含一种类型的数据。所以将矩阵强制转换为data.frame
。然后lapply
函数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
然后您可以使用 str
检查 data.frame
:
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