0

I have a list "ans" here are its few entries

[[93]]
[1] "<dd>male, 31 years old</dd>" "black male"                       
[3] "31"                               

[[94]]
character(0)

[[95]]
[1] "<dd>female, 23 years old</dd>" "female"                       
[3] "23"                               

[[96]]
[1] "<dd>male, 23 years old</dd>" "male"                       
[3] "23"  

I have to find if each entries matches a certain age

so I have a loop like this

for(item in ans){
    if(item[3] && item[3]==age){
         count = count + 1
    }
}

but their are some entries like

[[94]]
    character(0)

due to which I am getting a error : missing value where TRUE/FALSE needed

how do I get rid of this problem

4

1 回答 1

3

这个对我有用:

ans <- list(c("<dd>male, 31 years old</dd>", "black male", "31"  ), character(0))
count <- 0
age <- "31"
for(item in ans){
  if (isTRUE(item[3] == age)) {
    count <- count + 1
  }
}
count

这是你想要的吗?

于 2013-10-20T14:14:30.343 回答