r如果语句遇到错误:参数长度为零 [英] r if statement meet error: argument is of length zero

查看:173
本文介绍了r如果语句遇到错误:参数长度为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是数据:

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

后跟数字:"表示它是一个movieID,然后以下几行是customerID,我想编写一个循环来检查数据是否包含:",这是我尝试的代码:

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

运行此代码时,发生错误,错误是:参数长度为零我搜索了此错误,然后更改了if语句:

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 )

仍然存在错误:缺少需要TRUE/FALSE的值

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

我解决不了.这是我的代码:

I can't solve it. This is the code I:

for (i in 1:27){
  #print(testData[i])
  if(grep(':', testData[i])==1 ){
    movieID<-as.integer(substr(testData[i],1,nchar(testData[i])-1)  )
    print(testData[i])
    next
  }else{
    customerID<-as.integer(testData[i])
    print(movieID)
    print(customerID)
 #print(subset.data.frame(mydata[[movieID]],mydata[[movieID]]$customerID==customerID) )
  }
}

这里是输出和错误:

[1] "1:"
Error in if (grep(":", testData[i]) == 1) { : argument is of length zero

看起来错误发生在else语句上.

It looks like the error occur at else statement.

推荐答案

错误是因为如果您查找的字符串不是 grep 返回 logical(0)展示.因此,循环在 i = 2 上失败,正如您在循环中断时查看 i 的值所看到的那样.

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.

如果您改用 grepl ,则循环按计划进行(以@Akarsh Jain的答案为基础):

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])

  }
}

当然,问题是这有多有用.我假设您想以某种方式在movieID上拆分数据,您可以使用 dplyr tidyr 轻松地做到这一点:

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

输出:

    customerID movieID
1    30878       1
2  2647871       1
3  1283744       1

虚拟数据

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

这篇关于r如果语句遇到错误:参数长度为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆