Created
February 24, 2017 11:48
-
-
Save nathan-russell/d8d94faa4cb01d6a5481bc1d0725d50d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df1 <- data.table::data.table( | |
Country = c("Germany", "France"), | |
y = rnorm(10), | |
x = rnorm(10) | |
) | |
# Option 1: non-environment object + special assignment operator | |
failed.countries <- character() | |
for (country in c("France","USA", "Germany")) { | |
tryCatch({ | |
lm(y ~ x, data = df1[Country == country]) | |
}, error = function(e) { | |
failed.countries <<- c(failed.countries, country) | |
}) | |
} | |
failed.countries | |
# [1] "USA" | |
# Options 2: environment object + ordinary assignment operator | |
failed.countries <- new.env() | |
for (country in c("France","USA", "Germany")) { | |
tryCatch({ | |
lm(y ~ x, data = df1[Country == country]) | |
}, error = function(e) { | |
failed.countries[[country]] <- TRUE | |
}) | |
} | |
ls(failed.countries) | |
# [1] "USA" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment