. Advertisement .
..3..
. Advertisement .
..4..
I get an error
number of items to replace is not a multiple of replacement length
when I try to run the following code:
combi$DT[is.na(combi$DT) & ! is.na(combi$OD) ] <- combi$OD
id DT OD
67 2010-12-12 2010-12-12
68 NA NA
69 NA 2010-12-12
70 NA NA
How to fix r number of items to replace is not a multiple of replacement length. Please give me some good ideas.
The cause: The replacement length is not multiplied by the number of items that need to be replaced. The number of items you can replace is determined by the number is.na(combi$DT) & !is.na(combi$OD) rows that are less than combi rows (and the replacement length).
Solution: The easiest way to fix this error is to simply use ifelse:
N.B. the
and !is.na(combi$OD)
is redundant in the event that the two areNA
the substitute would beNA
. You can use onlyThis warning occurs because you’re trying to assign all
combi$OD
at thecombi$DT
NA locations. If you have 100 rows with 2 variables each with 5 NAs, you tell it to replace the 5 NAs in variable1 by 100 values. This is why the warning. This is a better alternative.