R for循环是数字

时间:2019-06-11 07:41:52

标签: r for-loop numeric edgar

我有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对,以便从一个站点下载年度报告。此循环需要数值变量才能成功下载,看来我没有得到它们。例如,编写以下循环(对于变量firm或year,使我都不是数值变量):

    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”似乎依赖于此。 预先谢谢你。

0 个答案:

没有答案