13

Ignore an error in an R-loop

 4 years ago
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.
neoserver,ios ssh client

Ignore an error in an R-loop

advertisements

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

Some background. I want to delete AWS Redshift cluster and the process takes more than 30 minute. So this is what I want to do: Start the deletion Every 1 minute, check the cluster status (it should be "deleting") When the cluster is deleted, th

How to remove / ignore the error / warning JSLINT for @ or sign?

How to set a option for jslint to ignore the error or warning for at-sign(@) object: { name: stackoverflow @href: http://stackoverflow/questions/ask } console.log(object.name + "...name" + object.@href + "...href"); When complied I get

Ignore insertion errors

I am loading from a mongodb table to mysql table using kettle 4.4 one of the column at some row gives an error : Incorrect string value: '\xF0\x9F\x98\x8D' for column. I checked some of the forums and modified the column to utf8mb4_general_ci , but s

How to ignore the error: System.Security.Cryptography.CryptographicException: Store Root does not exist

I'm using Xamarin for creating Monodroid app. My communication with backend is going over https protocol. That leads to "System.Security.Cryptography.CryptographicException: Store Root doesn't exists" error in debug mode which is quate annoying.

Qt ignores SSL error

I have had a look at several different attempts of ignoring SSL handshake errors in Qt but I have gotten none to work. I am using a self signed certificate and during development I would just like to ignore any error in case I have to change certs or

Remote WebDriver ignores certificate errors for Chrome

How ignore sertificate with Remote WebDriver for Chrome? I try run this code: #encoding: utf-8 require 'selenium-webdriver' include Selenium capabilities = WebDriver::Remote::Capabilities.chrome(:native_events => true) driver = WebDriver.for(:remote,

Is it possible to ignore specific errors in PHP?

During development, I use error_reporting(E_ALL | E_STRICT | E_NOTICE); Sometimes, though rarely, there are specific E_STRICT errors I'd like to ignore. But I don't want to turn off E_STRICT entirely. Is there a way to ignore specific errors, whether

Is there a way to ignore cache errors in Django?

I've just set our development Django site to use redis for a cache backend and it was all working fine. I brought down redis to see what would happen, and sure enough Django 404's due to cache backend behaviour. Either the Connection was refused, or

The '-ignore-certificate-errors' switch does not work for Chrome 15

I have an issue with automating Chrome 15. If I start Chrome from ruby script with --ignore-certificate-errors switch, I still get certificate error prompt. I start chrome with next command browser = Watir::Browser.new :chrome, :switches => ['--ignor

How do I ignore SSL errors in Qt5 QML QWebKit 3.0?

I've got a problem showing www.facebook.com in Qt5 QML QWebKit 3.0. The problem is with SSL certificate. There is a solution for Qt 4.*, but I need to implement this in the Qt 5.0.1 where all the API had changed. The example solution for Qt 4.* could

Chrome error: You use an unsupported command line flag: --ignore-certificate-errors. Stability and security will

Browser opens with the below mentioned line- You are using an unsupported command-line flag: --ignore-certifcate-errors. Stability and security will suffer. as well as after few second the browser close and the error is thrown. I am facing the above

Can I ignore the error without using try catch ()?

So I want to now if that possible for .NET languages to ignore error without using try catch construction ? I can ignore warring with #nowarring 40 for example, can I ignore error ? Why I want it ? simply wanted to call system pause with this way ope

Unsupported command line flag: --ignore-certificate-errors (in Ruby)

Using Ruby 2.0.0 p481 in RubyMine and chromedriver 2.10 When Chrome starts it displays a message in a yellow popup bar: "You are using an unsupported command-line flag: --ignore-certificate-errors. Stability and security will suffer." This simpl

How do I fix (or ignore) javascript errors in Visual Studio 2015

I have a solution whose startup project is an ASP.NET website (the kind without a .vbproj file) When I'm running in debug mode, the first error I get is a popup Unhandled exception at line 539, column 51 in script block 0x800a138a - JavaScript runtim

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK