I have a data frame with a numeric column called store that has some negative values. I’d like to add 1440 to the negative , but am having trouble. My data looks like this:
score
1 816
2 -200
3 976
4 -376
5 1
6 121
7 -331
I can replace the values using temp[temp$score< 0] <-8888.
But, I when I try to add value to the variable using: temp[temp$score < 0] <- temp$score + 1440, I get an warning that says:
Warning message: In temp$score[temp$score < 0] <- temp$score + 1440
:number of items to replace is not a multiple of replacement length
And then I get some odd values returned:
score
1 816
2 2256
3 976
4 1240
5 1
6 121
7 2416
Am I calling the function wrong or am I selecting the cases wrong?
From your warning message, it seems like you were trying to do the following:
The problem here is that you are replacing a vector with one that is a different length, as the warning message suggests. You shortened the left-hand side of the assignment, but not the right-hand side – the solution would be to shorten the right-hand side too, as follows: