Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - How to test for character(0) in IF statement

Tags:

r

if-statement

I imagine this is incredibly simple but I cant seem to find the answer.

I am writing an IF statement but the test is if the object returns a character(0) value. I am not sure how to deal with character(0) in the statement.

Assuming Test <- character(0) or a value:

if (identical(Pols4,character(0))) {   print('Empty') } else {   print('Not Empty') } 

It still does not seem to work.....

like image 578
Methexis Avatar asked Sep 03 '14 08:09

Methexis


People also ask

How do I check if a vector is empty in R?

If you check length(vec) , we know the length of our vector is also 0, meaning that our vector, vec , is empty.

Is empty in R?

To check if list is empty in R programming, we have to evaluate the condition that the length of list is zero. Call length() function with this list passed as argument and if the return value is 0 using equal to operator.

How do I make a character vector in R?

To create a character vector in R we can enclose the vector values in double quotation marks but if we want to use a data frame values to create a character vector then as. character function can be used. For example, if we have a data frame df then all the values in the df can form a character vector using as.


1 Answers

Use the identical function to check this.

a <- character(0) identical(a, character(0))  # returns TRUE  identical(a, "")           # returns FALSE identical(a, numeric(0))   # returns also FALSE 
like image 146
Patrick Roocks Avatar answered Sep 19 '22 13:09

Patrick Roocks