添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am having a little problem with R and I am not sure why. It is telling me that this line: if(temp > data[[k]][[k2]]) { is of argument length 0. Here is the block which is not that big:

for(k in 1:length(data)) { 
      temp <- 0
      for(k2 in 3:length(data[[k]])) {
           print(data[[k]][[k2]])
           if(temp > data[[k]][[k2]]) {
                temp <- data[[k]][[k2]]
            fMax[k] <- temp
           k2 <- k2 + 1
 k <- k + 1

example of what is in data[[k]][[k2]]:

[1] "3050"
[1] "3051"
[1] "3054"
[1] "3054"
[1] "3052"
[1] "3053"
[1] "3059"
[1] "3059"
[1] "3057"
[1] "3060"
[1] "3063"
[1] "3060"
[1] "3068"
[1] "3067"
[1] "3079"
[1] "3085"
[1] "3094"
[1] "3107"
[1] "3121"
[1] "3135"
[1] "3147"
[1] "3161"
[1] "3200"
[1] "3237"
[1] "3264"
[1] "3274"
[1] "3284"
[1] "3289"
[1] "3292"
[1] "3300"
[1] "3301"
[1] "3303"
[1] "3306"
[1] "3310"
[1] "3312"
[1] "3313"
[1] "3319"
[1] "3314"
[1] "3318"
[1] "3318"
[1] "3320"
[1] "3322"
[1] "3322"
[1] "3322"
[1] "3328"
[1] "3332"
[1] "3338"
[1] "3350"
[1] "3358"
[1] "3378"
[1] "3395"
[1] "3402"
[1] "3875"
[1] "3950"
[1] "3988"
[1] "4018"
[1] "4039"
[1] "4048"
[1] "4057"
[1] "4062"
[1] "4067"
[1] "4076"
[1] "4082"
[1] "4085"
[1] "4092"
[1] "4098"
[1] "4099"
[1] "4101"
[1] "4107"
[1] "4119"
[1] "4139"
[1] "4164"
[1] "4231"
[1] "4347"
[1] "4559"
                I tried a little bit to reproduce, but couldn't.  Try setting options(error=recover) to dump you in the browser when you hit the error.  Then print k and k2 and look carefully at the case that's getting you in trouble.
– Ben Bolker
                Dec 8, 2014 at 3:03
                You really should post a reproducible example with sample input and desired output that can be copy/pasted in to R. Also, describe what you're trying to do instead of just posting broken code; there may very well be a better way to do it. But your code is awfully odd to have a for loop over k2 and also so manually increment the value.
– MrFlick
                Dec 8, 2014 at 3:03
                Alright I put it as an answer with a good explanation, if its confusing let me know though I am not the best at explanations.
– user3558177
                Dec 8, 2014 at 3:13
                You don't need to manually increment a for loop, so get rid of the x <- x + 1. It's likely that for some k and some k2 there is no data[[k]][[k2]]
– Hugh
                Dec 8, 2014 at 3:30

"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.

Alternately, looking at the code: you're both looping with "for" and incrementing. The result is probably that you're going to end up referring to indices of data[[k]] that don't exist. Either use a for loop, or increment with a while loop, not both; for loops increment on their own. – Oliver Keyes Dec 8, 2014 at 14:30 NOTE: Replace && with & in the second code block. R's & operator does not short-circuit, so the suggested approach fails for the same reason as in the OP. – Chris Keefe Sep 9, 2020 at 23:42

You can use isTRUE for such cases. isTRUE is the same as { is.logical(x) && length(x) == 1 && !is.na(x) && x }

If you use shiny there you could use isTruthy which covers the following cases:

  • FALSE

  • An empty atomic vector

  • An atomic vector that contains only missing values

  • A logical vector that contains all FALSE or missing values

  • An object of class "try-error"

  • A value that represents an unclicked actionButton()

  • I was looping through field names to find column index numbers (x) and faced a similar issue when none found. is.null, is.logical and is.TRUE didnt see the zero lenght . Eventually I hit on if (length(x) > 0) – AnserGIS Aug 26, 2022 at 13:43

    I spent an entire day bashing my head against this, the solution turned out to be simple..

    R isn't zero-index.

    Every programming language that I've used before has it's data start at 0, R starts at 1. The result is an off-by-one error but in the opposite direction of the usual. going out of bounds on a data structure returns null and comparing null in an if statement gives the argument is of length zero error. The confusion started because the dataset doesn't contain any null, and starting at position [0] like any other pgramming language turned out to be out of bounds.

    Perhaps starting at 1 makes more sense to people with no programming experience (the target market for R?) but for a programmer is a real head scratcher if you're unaware of this.

    The argument is of length zero takes places when you get an output as an integer of length 0 and not a NULL output.i.e., integer(0).

    You can further verify my point by finding the class of your output- >class(output) "integer"

    The simplest solution to the problem is to change your for loop statement :

    Instead of using

          for (i in **0**:n))
    
          for (i in **1**:n))
    

    In my case, I just wanted to see the first position of the character as follows

    htagPos <- which(strsplit(val, "")[[1]] == "#") 
    if(htagPos == 1){
    }# this did now work:(
    

    So I had to check the length of the result first before checking the value

    htagPos <- which(strsplit(val, "")[[1]] == "#") 
    if(length(htagPos) >= 1 && htagPos == 1){
    
  • I see why most people prefer python...
  • So the other possibility for this error can be when the condition in IF is a return value from other function. For example,

    check <- function (value) {
    if (value == 0) {
     return TRUE
    If this function is called like this:

    if(check(value)) {
      do something
    

    So here, let's assume the value is not 0, there is no return statement for that case. In this case too, you'll get "argument is of length zero" error.

    Hope this is helpful!

    String data structures have the last data addressed nulled so use max(data) instead of data[last]. https://www.geeksforgeeks.org/string-data-structure/

    For example,a string with 4 elements will have a number element in it's 5th element.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.