folks, I encountered a issue when make logical judgement with R. Actually, it should be quite intuitive. While I have no idea.
I have a dataframe "dat" and one int var of "dat" is named "A", and I'd like to create another var "B" based on the value of "A", eg, when A = 16, 17 or 18, I need to assign B to be 6. however when I run "dat$B[dat$A == 16:18] <- 6", I got NA (missing values) in B even when A is actually 16 or 17 or 18. Then I checked by specifying the following lines, and the result seems quite weird.
individually, 15 + 15 + 15, while in total it is 39, how could that happen?! It should be 45! (Actually, I got 6 NA in B)
I'd like to know why this could happen, cuz when I assign the value of B based on other values of A, such as "dat$B[dat$A == 1:3] <- 1" it works fine. Besides, A is classified as integer, and I've checked any other related lines and they're also fine, finally, I located the issue below (with the line when making logical conditions judges with sequence numbers).
Has anybody have any idea about this, and why it happened? Is there a better way I can use when doing logical condition with a bunch of numbers (more than one)? Thanks a lot!
sum(dat$A== 16)
[1] 15
sum(dat$A == 17)
[1] 15
sum(dat$A == 18)
[1] 15
sum(dat$A== 16:18)
[1] 39