I am new to R language, and I am trying to avoid for loops in my program, so I use sapply() or ifelse(), but I meet some problems. Here is my program,
rowNum <- 1
txn$'ACCT_NUM' <- sapply(c(1:nrow(txn)), function(x) if(txn$'ACCT_ID'[x]==MKAccts$'ACCT_ID'[rowNum]){
return(MKAccts$'ACCT_NUM'[rowNum])
}else{
rowNum <- rowNum +1
print(rowNum)
return(MKAccts$'ACCT_NUM'[rowNum])
})
and I found that every time in the sapply() function, when it calls the rowNum, it will be set back to 1 again, I think it might be caused by the reason that it is just be called, the change in the function in sapply() actually dose not reflect to the value out of the function, but is there anyway to realize this by using sapply() or ifelse()? Thank you!