

Ignore an error in an R-loop
source link: https://www.codesd.com/item/ignore-an-error-in-an-r-loop.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Ignore an error in an R-loop
I have a dummy script below:
a <- 1
b <- 2
c <- 3
e <- 5
list <- letters[1:5]
for (loop in (1:length(list)))
{print(paste(list[loop],get(list[loop]),sep="-"))
}
> source('~/.active-rstudio-document')
[1] "a-1"
[1] "b-2"
[1] "c-3"
Error in get(list[loop]) : object 'd' not found
Currently I have a problem that since d
is not present, so an error message pop up and block the processing of e
.
I wonder if R has some kind of "error handling", that is to bypass the error due to d
, keep processing e
, then return the error message when all valid data are processed.
Thanks.
Use exists
to check whether a variable exists:
for (loop in (1:length(list))){
if(exists(list[loop])){
print(
paste(list[loop], get(list[loop]), sep="-"))
}
}
[1] "a-1"
[1] "b-2"
[1] "c-3"
[1] "e-5"
More generally, R has a sophisticated mechanism for catching and dealing with errors. See ?tryCatch
and its simplified wrapper, ?try
, for more details.
Related Articles
How to ignore the error and continue with the rest of the script
How to remove / ignore the error / warning JSLINT for @ or sign?
Ignore insertion errors
How to ignore the error: System.Security.Cryptography.CryptographicException: Store Root does not exist
Qt ignores SSL error
Remote WebDriver ignores certificate errors for Chrome
Is it possible to ignore specific errors in PHP?
Is there a way to ignore cache errors in Django?
The '-ignore-certificate-errors' switch does not work for Chrome 15
How do I ignore SSL errors in Qt5 QML QWebKit 3.0?
Chrome error: You use an unsupported command line flag: --ignore-certificate-errors. Stability and security will
Can I ignore the error without using try catch ()?
Unsupported command line flag: --ignore-certificate-errors (in Ruby)
How do I fix (or ignore) javascript errors in Visual Studio 2015
Recommend
-
30
An issue was recently raised on the ConTabs project that sent me down a bit of a localisation rabbit hole. You see, I’d written a load of conformance tests that included example output. What I hadn’t factored in was how m...
-
49
README.md overreacted.io Maybe I'll blog
-
35
The IGNORE_DUP_KEY option for unique indexes specifies how SQL Server responds to an attempt to INSERT duplicate values: It only applies to tables (not views) and only to inserts. Any insert por...
-
63
r/git:
-
40
git-ignore-command A custom command for git to manage the gitignore file (with help from gitignore.io) Installation Copy the file git-ignore somewhere on your PATH so that it can be f...
-
55
You can set a file or folder to be “ignored” by Dropbox. This allows you to organize files and folders in the Dropbox folder on your computer without storing them on dropbox.com or on the Dropbox server at all. Onc...
-
4
Ignore All Web Performance Benchmarks, Including This One A couple of months ago there was an article titled
-
8
Don't ignore bandwidth limits May 14, 2020 I never thought much about how much bandwidth network resources have access to when spinning up infrastructure. I just assumed everything is “fast enough...
-
57
Intel DX79SI/TO Error 67 Boot Loophidden text to trigger early load of fonts ПродукцияПродукцияПродукцияПродукция Các sản phẩmCác sản phẩmCác sản phẩmCác sản phẩm المنتجاتالمنتج...
-
6
Runtime Error for reverse Loop Runtime Error for reverse Loop
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK