简体   繁体   中英

Programming error “argument is of length zero” %in%

I get a continuous error in the first "%in%" statement. I have checked the other "argument of length zero" answers and didn't see any that applied.

Testing just data$var[1] %in% Group1Zips returns FALSE in the R console, so I must be missing something beyond a NULL value being returned.

Any help would be greatly appreciated.

    NewColumn= vector() 
    builder = function(data, TRXVAR, ZIPVAR){

    Group1 <- 0
    Group1Zips = vector()
    Group2 <- 0
    Group2Zips = vector()
    Group3 <- 0
    Group3Zips = vector()
    Group4 <- 0 
    Group4Zips = vector()
    Group5 <- 0 
    Group5Zips = vector()
    for (i in 1:nrow(data)){
    if (data$ZIPVAR[i] %in% Group1Zips){
    Group1 = Group1 + TRXVAR[i]
    append(NewColumn,"Group1")
    break }
      else if (data[i, ZIPVAR] %in% Group2Zips){
    Group2 = Group1 + data[i, TRXVAR]
    append(NewColumn, "Group2")
    break
  }
  else if (data[i, ZIPVAR] %in% Group3Zips){
    Group3 = Group3 + data[i, TRXVAR]
    append(NewColumn, "Group3")
    break
  }
  else if (data[i, ZIPVAR] %in% Group4Zips){
    Group4 = Group4 + data[i, TRXVAR]
    append(NewColumn, "Group4")
    break
  }
  else if (data[i, ZIPVAR] %in% Group5Zips){
    Group5 = Group5 + data[i, TRXVAR]
    append(NewColumn, "Group5")
    break
  }
  else if (Group1 < Group2){
    Group1 = Group1 + data[i, TRXVAR] 
    append(Group1Zips, data[i, ZIPVAR])
    append(NewColumn, "Group1")
    break
  }
  else if (Group2 < Group3){
    Group2 = Group2 + data[i, TRXVAR]
    append(Group2Zips, data[i, ZIPVAR])
    append(NewColumn, "Group2")
    break
    }
  else if (Group3 < Group4){
    Group3 = Group3 + data[i, TRXVAR]
    append(Group3Zips, data[i, ZIPVAR])
    append(NewColumn, "Group3")
    break
    }
  else if (Group4 < Group5){
    Group4 = Group4 + data[i, TRXVAR]
    append(Group4Zips, data[i, ZIPVAR])
    append(NewColumn, "Group4")
    break
    }
   else if (Group5 < Group1){
    Group5 = Group5 + data[i, TRXVAR]
    append(Group5Zips, data[i, ZIPVAR])
    append(NewColumn, "Group5")
    break
    }
  else {
    Group1 = Group1 + data[i, TRXVAR]
    append(Group1Zips, data[i, ZIPVAR])
    append(NewColumn, "Group1")
  }
 }
 return(nrow(NewColumn))
}

edit**

Here's a sample of the data:

    PROVIDER.ID ZIP TRX_ALL SPEC_CODE   
    2432188006  10013   4331    NEP 
    0050676082  90012   3050    IM  
    4954985007  77479   3043    CD  
    0250771087  90031   3020    OPH 

Your code is missing a "}" somewhere, and I don't think you give enough data to reproduce this, but it looks a lot like your if statement should be:

if(data[i, ZIPVAR] %in% Group1Zips){

The way you've written it will be looking for a column called "ZIPVAR" in any object you give it, instead of looking for a column named with the value of ZIPVAR.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM