Reputation: 11
When running my forloop function, I came across the following error message: "Error in [.data.frame
(returns, i:(i + 60), c(FALSE, ESGscores_logical)) : undefined columns selected". I have tried many different alterations but nothing seems to be working.
The error arises in the "returns_subset", where I would like to filter the correct 61 months and stocks in the 80%.
I have previously built the ESGscores_subset with all of the ESG scores in one year.
And then created the ESGscores_logical dataset with marking TRUE when the stock is in the higher 80%.
Here is my data:
as.data.frame(ESGscores)
year | Stock A | Stock B | Stock C |
---|---|---|---|
1 | 20 | 33 | 45 |
2 | 40 | 23 | 55 |
3 | 45 | 45 | 44 |
4 | 55 | 50 | 45 |
as.data.frame(returns)
year | Month | Stock A | Stock B | Stock C |
---|---|---|---|---|
1 | 1 | 0.033 | 0.045 | 0.066 |
1 | 2 | 0.0023 | 0.0055 | 0.07 |
1 | 3 | 0.035 | 0.00344 | 0.033 |
1 | 4 | 0.004 | 0.005 | 0.04 |
ESGscores_subset
year | Month | Stock A | Stock B | Stock C |
---|---|---|---|---|
1 | 1 | 0.033 | 0.045 | 0.066 |
ESGscores_logical
Stock A | Stock B | Stock C |
---|---|---|
TRUE | FALSE | FALSE |
Here is my code:
for(i in 1:60) {
year <- returns[i+60,"Year"]
ESGscores_subset <- ESGscores[ESGscores$year == year-1, 2:411]
ESGscores_logical <- as.numeric(ESGscores_subset) >=
as.numeric(quantile(ESGscores_subset, 0.8))
returns_subset <- returns[i:(i+60), c(FALSE, ESGscores_logical)]
}
I am really stuck, your help would be much appreciated!
Upvotes: 1
Views: 50