0

As the new user in R, I met few problems when I tried to evaluate a.

Code:

// time2 are numbers //
a = d3$Time2  
b = c(...)
for (i in 1:65){
  for (j in 1:1762){
    if( (a[j]>=1474161415+900*(i-1))&(a[j]<1474161415+900*i) ){ a[j] = b[j] }
  }
}

Results:

Error in if ((a[j] >= 1474161415 + 900 * (i - 1)) & (a[j] < 1474161415 + : 
missing value where TRUE/FALSE needed

Also I have tried:

ifelse( ((a[j]>=1474161415+900*(i-1)) & (a[j]<1474161415+900*i)) , a[j]=b[j])

Results:

-unexpected '=' in -ifelse( ((a[j]-=1474161415+900-(i-1)) & 
    (a[j]-1474161415+900-i)) , a[j]=--
gung - Reinstate Monica
  • 11,583
  • 7
  • 60
  • 79
H.Chen
  • 9
  • 1
  • Please provide the data to make this reproducible. If I make up fake data your loop runs without error, so the answer will depend on your data. If you have `NA` values for instance, then that's the problem. – Hack-R Jul 19 '17 at 15:12
  • Without seeing your data, it's possible you're having issues with `&`, which is vectorized. You might replace with `&&` which I believe is better suited for `if` statements, though others here know much more about that than I do. See here for more info: https://stackoverflow.com/questions/6558921/r-boolean-operators-and – MeetMrMet Jul 19 '17 at 15:14
  • Using `&` in an `if` expression is a red flag, since `if` only uses the first element of a logical vector. Also note that `ifelse` does not conditionally run code. It returns one of two values for each vector index. I think you want this: `a = ifelse(a > ... & a < ..., b, a)` – Matthew Lundberg Jul 19 '17 at 15:20
  • 1. Time3 data: 1474379938, 1474372322, 1474332442..., I also used is.na() to check, there were no NA values. 2. In this case, && or & were tried, but the error were the same. 3. ifelse will return two values, but I just need the "TRUE returns", the data to be covered by the "FALSE return". 4. dim(d3) = 1762 9 – H.Chen Jul 19 '17 at 15:35
  • 1
    The other red-flag here is the failure to have both of the loop indices in the "interior assignment operations:. It means the `a` values will be overwritten 65 times. – IRTFM Jul 19 '17 at 16:08

0 Answers0