0

I have a "test" dataframe with 3 companies (ciknum variable) and years in which each company filed annual reports (fileyear):

   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

These two columns are numeric:

 > is.numeric(test$ciknum)
[1] TRUE
> is.numeric(test$fileyear)
[1] TRUE

However, I need a loop that goes for each ciknum-fileyear pair to download annual reports from one site. This loop requires numeric variables for successful download, and it seems I don't get them. For instance, writing the following loop (either for the variable firm, or year, gives me that none are numeric variables):

    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)

I checked whether new df firm and year are numeric, and there is mixed evidence. On the one hand, it seems it reads year as numeric variable:

> 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"

But on the other hand, it seems it does not:

    > 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"

Can anyone tell me whether these are numeric variables or not? Getting lost on this one... My download function "my_getFilings" seems to depend on that. Thank you in advance.

martins
  • 441
  • 1
  • 5
  • 19
  • 1
    Those numbers might be stored as factors. This could be checked with `is.factor()`. If that is the case, you can find several posts on SO on how to convert factors to numeric values. – RHertel Jun 11 '19 at 07:48
  • Using your `test` data and `for` loop code I cannot reproduce your `"is not numeric"` output in second `for` loop (It prints nothing). Rest all shows the same output as you. – Ronak Shah Jun 11 '19 at 07:50
  • 1
    Consider using `year <- as.numeric(test[row, "fileyear"])`. If it's a factor you'll have to do `year <- as.numeric(as.character(test[row, "fileyear"]))` – Dave Jun 11 '19 at 08:52
  • What does str(test) tell you? – Roland Jun 11 '19 at 09:14
  • Thanks guys. @Roland: str(test) gives me: $ ciknum : num 1408356 1557255 1557255 1557255 1557255 ... $ fileyear: num 2013 2013 2014 2015 2016 ... – martins Jun 11 '19 at 09:33

0 Answers0