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/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.
R argument is of length zero when using names(vector) in IF statement
Your function uses the line
Rvec = as.vector(Rvec)
The help documentation for as.vector()
states that "if the result is atomic, all attributes are removed." This means that element names are dropped when you run as.vector()
.
One solution is not to coerce Rvec to be a vector, but rather to check that it is a vector. You'll also need to change the if()
statement in your third line to either an ifelse()
or an all.equal()
. Here's one solution with those 2 fixes (replace the ... with the rest of your code):
check.names <- function(Rvec,Data){
if(!is.vector(Rvec)) stop("Rvec must be a vector")
if(!is.data.frame(Data)) stop("Data must be a data.frame")
dnames <- names(Data)[3:ncol(Data)]
rnames <- names(Rvec)[3:length(Rvec)]
if(!all.equal(dnames, rnames)) stop("Names don't match")
...
}
R argument of length zero in if statement
The issue is most likely due to creation of unused levels
because by default drop = FALSE
in interaction
df$id_year <- interaction(df$id, df$year, drop = TRUE)
In the loop, as others have mentioned the !unique(
is not clear. If we are checking the length
of unique
elements are greater than 0 (if (length(unique(df$main[rows])) > 0)
, it would print "yes"
for (i in levels(df$id_year)) {
rows <- df$id_year == i & df$usable
if (length(unique(df$main[rows])) > 0) {
print("yes")
}
}
#[1] "yes"
#[1] "yes"
#[1] "yes"
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
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
r if statement meet error: argument is of length zero
the error is because grep
returns logical(0)
if the string you are looking for is not present. So your loop fails on i=2
, as you can see when you look at the value of i
when the loop breaks.
If you use grepl
in stead, your loop works as planned (building on @Akarsh Jain s answer):
movieID<-array()
customerID<-array()
for (i in 1:length(testData)){
if(grepl(':', testData[i])){
movieID[i]<-as.integer(substr(testData[i],1,nchar(testData[i])-1) )
next
} else{
customerID[i]<-as.integer(testData[i])
}
}
ofcourse, the question is how useful this is. I assume you want to somehow split your data on movieID, which you can do easily using dplyr
and tidyr
:
library(dplyr)
library(tidyr)
#put your testData in a dataframe
testDf <- data.frame(customerID = testData)
newDf <- testDf %>%
#identify rows with :
mutate(movieID = ifelse(grepl(":",customerID), customerID, NA)) %>%
#fill all NA values in movieID with the previous non-NA value:
fill(movieID) %>%
#remove lines where customerID has a ":":
filter(!grepl(":",customerID))
output:
customerID movieID
1 30878 1
2 2647871 1
3 1283744 1
dummy data
testData <- read.table(text='1:
30878
2647871
1283744
2488120
317050
1904905
1989766
14756
1027056
1149588
1394012
1406595
2529547
1682104
2625019
2603381
1774623
470861
712610
1772839
1059319
2380848
548064
10:
1952305
1531863
1000:
2326571
977808
1010534
1861759
79755
98259
1960212
97460
2623506
2409123', stringsAsFactors=FALSE)[[1]]
if statement get rid of: Error in if: argument is of length zero
The answer including browser()
function was indeed helpful for locating the source of the problem but the real solution is to use req()
or a combination of validate()
and need()
functions. See respective help()
pages to learn how to use them.
In short what these functions do is to check whether the argument given has been specified. In reference to the example code the solution could look like this:
server <- function(input, output, session){
output$output <- renderUI({
# check if input$input is specified
# if not stop executing the rest of this code block
req(input$input)
div(
# show name
data[which(input$input == data[, "name"]), "name"], br(),
data[which(input$input == data[, "name"]), "age"], br(),
# if "coward" is not FALSE than show its value
if(data[which(input$input == data[, "name"]), "coward"] != FALSE){
data[which(input$input == data[, "name"]), "coward"]
}
)
})
}
Related Topics
How to Use Objects from Global Environment in Rstudio Markdown
Dealing with True, False, Na and Nan
Split Text String in a Data.Table Columns
How to Count the Frequency of a String for Each Row in R
Read a Utf-8 Text File with Bom
Why Is Enquo + !! Preferable to Substitute + Eval
Options for Caching/Memoization/Hashing in R
Split Up '...' Arguments and Distribute to Multiple Functions
How to Round Up to the Nearest 10 (Or 100 or X)
Is Set.Seed Consistent Over Different Versions of R (And Ubuntu)
Replacing Numbers Within a Range with a Factor
Convert Named Character Vector to Data.Frame