How to Catch Integer(0)

How to catch integer(0)?

That is R's way of printing a zero length vector (an integer one), so you could test for a being of length 0:

R> length(a)
[1] 0

It might be worth rethinking the strategy you are using to identify which elements you want, but without further specific details it is difficult to suggest an alternative strategy.

Check if value == integer(0) in R

You can use identical which is the safe and reliable way to test two objects for being exactly equal (from the docs):

value = integer(0)

identical(value, integer(0))
# [1] TRUE

Or do the following check:

is.integer(value) && length(value) == 0
# [1] TRUE

Why is integer(0) outputted when I use the which() function?

integer(0) means there are no elements of your data frame that satisfy the conditions. (You could try

with(df, any(95 <= V5 & V5 <= 105 & 
13 <= V6 & V6 <= 17))

(edited on the basis of @H1's comment, to match your description rather than your code); rearranging slightly to approximate the A < B < C syntax that R's parser can't handle ...)

You should probably check str(df) and/or summary(df) (or sapply(df, class)) to make sure that your data frame has really been read in as intended (or use dplyr::read_csv(), which prints information about the classes inferred from the data set. In particular, any typos in your data that make an entry not be a valid number (extra decimal point, missing value such as "?" not recognized as missing, etc.) will make R interpret the entire column as a character (since you've set stringsAsFactors=FALSE) rather than a numeric variable.

If you want to force columns 2-14 to numeric, you can use df[-1] <- lapply(df[-1], as.numeric) however, it would be better practice to find and fix any problems upstream ...

Replace integer(0) by NA

Here's a way to (a) replace integer(0) with NA and (b) transform the list into a vector.

# a regular data frame
> dat <- data.frame(x = 1:4)
# add a list including integer(0) as a column
> dat$col <- list(45,
+ 64,
+ integer(0),
+ 78)
> str(dat)
'data.frame': 4 obs. of 2 variables:
$ x : int 1 2 3 4
$ col:List of 4
..$ : num 45
..$ : num 64
..$ : int
..$ : num 78
# find zero-length values
> idx <- !(sapply(dat$col, length))
# replace these values with NA
> dat$col[idx] <- NA
# transform list to vector
> dat$col <- unlist(dat$col)
# now the data frame contains vector columns only
> str(dat)
'data.frame': 4 obs. of 2 variables:
$ x : int 1 2 3 4
$ col: num 45 64 NA 78

How to check whether an Integer is null or zero in Java?

Since StringUtils class is mentioned in the question, I assume that Apache Commons lib is already used in the project.

Then you can use the following:

if (0 != ObjectUtils.defaultIfNull(myInteger, 0)) { ... }

Or using static import:

if (0 != defaultIfNull(myInteger, 0)) { ... }

Python Verifying if input is int and greater than 0

You need to convert input to an integer and then check that value. Since the user may input garbage, you have to handle the integer conversion by catching a value exception.

def get_amount():
while True:
amount = input("Enter amount: ")
try:
val = int(amount)
if val >= 0:
break
else:
print("Amount can't be negative, try again")
except ValueError:
print("Amount must be a number, try again")
return val

To check an integer more than 0

Don't throw a NumberFormatException, because there was nothing wrong with the format if the string - it was simply out of range. The standard, and correct, exception to throw is IllegalArgumentException with a message. And as a general rule (which applies here), you shouldn't catch an excrotion, but do nothing with it.

Try this:

public void setId(String id) {
if (Integer.parseInt(id) < 1){
throw new IllegalArgumentException("id must be greater than zero:" id);
}
this.id = id;
}

Also:

  • removed the int variable since you don't use it exception for comparing
  • removed the try catch - just let the exception bubble out. If the client passed bad dara that's their problem

How to catch integer divide by zero and access violation exceptions in native C++

It's strange not to have the possibility to catch these exceptions. but if it's like that, than i guess you can put your own validations and throw your own exceptions. But again, you may be missing something...

logical(0) in if statement

logical(0) is a vector of base type logical with 0 length. You're getting this because your asking which elements of this vector equal 0:

> !is.na(c(NA, NA, NA))
[1] FALSE FALSE FALSE
> which(!is.na(c(NA, NA, NA))) == 0
logical(0)

In the next line, you're asking if that zero length vector logical(0) is equal to 0, which it isn't. You're getting an error because you can't compare a vector of 0 length with a scalar.

Instead you could check whether the length of that first vector is 0:

if(length(which(!is.na(c(NA,NA,NA)))) == 0){print('TRUE')}

How do I cast a string to integer and have 0 in case of error in the cast with PostgreSQL?

I was just wrestling with a similar problem myself, but didn't want the overhead of a function. I came up with the following query:

SELECT myfield::integer FROM mytable WHERE myfield ~ E'^\\d+$';

Postgres shortcuts its conditionals, so you shouldn't get any non-integers hitting your ::integer cast. It also handles NULL values (they won't match the regexp).

If you want zeros instead of not selecting, then a CASE statement should work:

SELECT CASE WHEN myfield~E'^\\d+$' THEN myfield::integer ELSE 0 END FROM mytable;


Related Topics



Leave a reply



Submit