jeudi 4 mai 2017

r if statement meet error: argument is of length zero

Here is the data:

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

The number followed by ':' means it is a movieID, and then the following several lines are customerID, I want to write a loop to check whether the data contain ':', here is the code I tried:

for (i in 1:length(line)){
  #print(line[i])
  if(grep(':', line[i])==1 ){
    movieID<-as.integer(substr(line[i],1,nchar(line[i])-1)  )
    next
  } 
  else{
    customerID<-as.integer(line[i])
    #do something
  }
}

When I run this code, an error occurred, the error is: argument is of length zero I searched this error, then I changed the if statement:

if( !is.na(line[i]) && nchar(line[i])>1 && grep(':', line[i])==1 )

There is still an error: missing value where TRUE/FALSE needed

I can't solve it.

Aucun commentaire:

Enregistrer un commentaire