我有一个“测试”数据框,其中包含 3 家公司(ciknum 变量)和每家公司提交年度报告的年份(文件年):
ciknum fileyear
1 1408356 2013
2 1557255 2013
3 1557255 2014
4 1557255 2015
5 1557255 2016
6 1557255 2017
7 1555538 2014
8 1555538 2015
9 1555538 2016
10 1555538 2017
这两列是数字:
> is.numeric(test$ciknum)
[1] TRUE
> is.numeric(test$fileyear)
[1] TRUE
但是,我需要一个循环,用于每个 ciknum-fileyear 对从一个站点下载年度报告。这个循环需要数字变量才能成功下载,而且我似乎没有得到它们。例如,编写以下循环(对于变量公司或年份,我都知道没有一个是数字变量):
for (row in 1:nrow(test)){
firm <- test[row, "ciknum"]
year <- test[row, "fileyear"]
my_getFilings(firm, '10-K', year, downl.permit="y") #download function over firm-year
}
Error: Input year(s) is not numeric #error repeated 10 times (one per row)
我检查了新的 df 公司和年份是否是数字,并且有不同的证据。一方面,它似乎将年份读取为数字变量:
> for (row in 1:nrow(test)){
+ firm <- test[row, "ciknum"]
+ year <- test[row, "fileyear"]
+
+ if(year>2015) {
+ print(paste("I have this", firm, "showing a numeric", year))
+ }
+ }
[1] "I have this 1557255 showing a numeric 2016" #it only states years>2015. Seems it reads a number
[1] "I have this 1557255 showing a numeric 2017"
[1] "I have this 1555538 showing a numeric 2016"
[1] "I have this 1555538 showing a numeric 2017"
但另一方面,它似乎没有:
> for (row in 1:nrow(test)){
+ firm <- test[row, "ciknum"]
+ year <- test[row, "fileyear"]
+
+ if(!is.numeric(year)) {
+ print(paste("is not numeric"))
+ }
+ }
[1] "is not numeric"
[1] "is not numeric"
[1] "is not numeric"
[1] "is not numeric"
[1] "is not numeric"
[1] "is not numeric"
[1] "is not numeric"
[1] "is not numeric"
[1] "is not numeric"
[1] "is not numeric"
谁能告诉我这些是否是数字变量?迷失在这个...我的下载功能“my_getFilings”似乎依赖于此。先感谢您。