Error in If/While (Condition) {:Argument Is of Length Zero

Error in if condition {: argument is of length zero

I'd suggest researching dataframe subsetting as your missing out on a substantial benefit of r.

df$Status <- 0
df$Status[df$End_week == 1426] <- 1

Error in if/while (condition) { : argument is of length zero

See ?NULL

You have to use is.null

‘is.null’ returns ‘TRUE’ if its argument is ‘NULL’ and ‘FALSE’
otherwise.

Try this:

if ( is.null(hic.data[[z]]) ) { print("is null")}

From section 2.1.6 of the R Language Definition

There is a special object called NULL. It is used whenever there is a need to indicate or
specify that an object is absent. It should not be confused with a vector or list of zero
length.
The NULL object has no type and no modifiable properties. There is only one NULL object
in R, to which all instances refer. To test for NULL use is.null. You cannot set attributes
on NULL.

Argument is of length zero in if statement

"argument is of length zero" is a very specific problem that comes from one of my least-liked elements of R. Let me demonstrate the problem:

> FALSE == "turnip"
[1] FALSE
> TRUE == "turnip"
[1] FALSE
> NA == "turnip"
[1] NA
> NULL == "turnip"
logical(0)

As you can see, comparisons to a NULL not only don't produce a boolean value, they don't produce a value at all - and control flows tend to expect that a check will produce some kind of output. When they produce a zero-length output... "argument is of length zero".

(I have a very long rant about why this infuriates me so much. It can wait.)

So, my question; what's the output of sum(is.null(data[[k]]))? If it's not 0, you have NULL values embedded in your dataset and will need to either remove the relevant rows, or change the check to

if(!is.null(data[[k]][[k2]]) & temp > data[[k]][[k2]]){
#do stuff
}

Hopefully that helps; it's hard to tell without the entire dataset. If it doesn't help, and the problem is not a NULL value getting in somewhere, I'm afraid I have no idea.

error in if() argument is of length zero in R

To detail a bit my comment and @Adii_ 's :

when you use grep, the result is the "position" of elements that fulfill the condition... so "nothing" if there is no match (hence the error message).

Using grepl, you'll get TRUE or FALSE, which you can use in your if statement.

As for length(grep(...)), the result will be 0 if there is no match, corresponding to FALSE for the if statement, or a positive integer (1 in your case because you're testing only one element), if there is a match, corresponding to TRUE for the if statement.

if statement in R. Error argument is of length zero

You have to extract the data from the xts object first

library(quantmod) 

xts_object <- getSymbols("000001.SZ", from= '2016-1-1', src="yahoo", auto.assign = F)
temp <- coredata(xts_object)

for (i in 101:length(temp[,6]))
{
if ( temp[i-1,5]<temp[i,5] )
print(temp[i,])
}

argument is of length zero in if statement

Your query can be illustrated with the following example:

grep(pattern="W","huh")
# integer(0)

No match results in a vector of length 0, hence the error. Instead use grepl, i.e. if( grepl( "W" , y ) ).

grepl has the return value TRUE or FALSE.

As a side note, eval( parse( "sometext" ) ) is variously thought of as not a good idea. You could try using the following untidy lapply statement instead (which will be better than apply because you don't have to convert to a matrix first):

data.frame( lapply( data , function(x) 
ifelse( grepl("W",x) ,
as.integer( gsub("W","",x) ) * 2L ,
x ) ) )
# V1 V2 V3 V4
#1 2 16 4 16
#2 1 16 4 0
#3 2 16 1 0
#4 3 64 3 0

Receiving error while trying to loop an if condition: argument is of length zero

The issue is that grep returns an index rather than a logical value. You want to use grepl which will return a logical value. See the grep documentation.

edit:
A couple things can cause your followup error:

  • geoplaces and/or addresses contain less than 8 rows of data
  • geoplaces contains NA values that need to be handled before making the grepl comparison (R thinks the search pattern is undefined)

It's impossible to say without having the complete dataset. The base functions is.na and nrow can help deal with either possibility.

Error in if/while (condition) {: missing Value where TRUE/FALSE needed

The evaluation of condition resulted in an NA. The if conditional must have either a TRUE or FALSE result.

if (NA) {}
## Error in if (NA) { : missing value where TRUE/FALSE needed

This can happen accidentally as the results of calculations:

if(TRUE && sqrt(-1)) {}
## Error in if (TRUE && sqrt(-1)) { : missing value where TRUE/FALSE needed

To test whether an object is missing use is.na(x) rather than x == NA.


See also the related errors:

Error in if/while (condition) { : argument is of length zero

Error in if/while (condition) : argument is not interpretable as logical

if (NULL) {}
## Error in if (NULL) { : argument is of length zero

if ("not logical") {}
## Error: argument is not interpretable as logical

if (c(TRUE, FALSE)) {}
## Warning message:
## the condition has length > 1 and only the first element will be used


Related Topics



Leave a reply



Submit