0

I have a little problem with a loop. Here is the code of the loop:

for(i in 2 : seq_along(dfpl$jobid_c)){
  if ((dfpl$jobid_c[i]>0) & (dfpl$job_id[i]==0)  ){
    j = (i-1)
    dfpl$formula_code[i] = dfpl$formula_code[j] 
    dfpl$formula_date[i] = dfpl$formula_date[j] 
    dfpl$formula_name[i] = dfpl$formula_name[j]
  }
  else{ 
    
  }
}  
Error in dfpl$formula_code[i] <- dfpl$formula_code[j] : 
  replacement has length zero
In addition: Warning message:
In 2:seq_along(dfpl$jobid_c) :
  numerical expression has 4205 elements: only the first used
monte
  • 1,482
  • 1
  • 10
  • 26
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jul 18 '20 at 03:04
  • 2
    Here is your first problem: `2 : seq_along(dfpl$jobid_c)`. Since `seq_along()` generates a vector from 1 to the length of `jobid_c` and 2: begins a vector with 2. R does not know what to do so it just uses the 2. You probably want `2:length(dfpl$jobid_c`. – dcarlson Jul 18 '20 at 03:47

0 Answers0