Skip to content

Instantly share code, notes, and snippets.

@nathan-russell
Created February 24, 2017 11:48
Show Gist options
  • Save nathan-russell/d8d94faa4cb01d6a5481bc1d0725d50d to your computer and use it in GitHub Desktop.
Save nathan-russell/d8d94faa4cb01d6a5481bc1d0725d50d to your computer and use it in GitHub Desktop.
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