Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get "number of items to replace is not a multiple of replacement length"

Tags:

dataframe

r

I have a dataframe combi including two variables DT and OD.

I have a few missing values NA in both DT and OD but not necessary the same record.

I then try to replace missing values in DT with OD if OD not is missing but retrieve the warning "number of items to replace is not a multiple of replacement length". I can see it means a mismatch in length, but I dont understand why two columns in the same dataframe can have different length. More seriously the output is no fully correct (see below)

combi$DT[is.na(combi$DT) & ! is.na(combi$OD) ] <- combi$OD 

Output

id   DT           OD 67   2010-12-12   2010-12-12 68   NA           NA 69   NA           2010-12-12 70   NA           NA 

I would have expected DT to be 2010-12-12 for id=69 (Dates are POSIXct).

There must be something I dont understand of length in dataframes. Anybody can help?

like image 910
Jørgen K. Kanters Avatar asked Aug 03 '16 08:08

Jørgen K. Kanters


2 Answers

Because the number of items to replace is not a multiple of replacement length. The number of items to replace is the number of rows where is.na(combi$DT) & !is.na(combi$OD) which is less than the number of rows in combi (and thus the length of the replacement).

You should use ifelse:

combi$DT <- ifelse(is.na(combi$DT) & !is.na(combi$OD), combi$OD, combi$DT) 

N.B. the & !is.na(combi$OD) is redundant: if both are NA, the replacement will be NA. So you can just use

combi$DT <- ifelse(is.na(combi$DT), combi$OD, combi$DT) 
like image 148
Hugh Avatar answered Sep 20 '22 17:09

Hugh


The warning is produced because you are trying to assign all combi$OD to the places where combi$DT is NA. For example if you have 100 rows of 2 variables with 5 NAs, then you are telling it to replace those 5 NAs of variable1 with the 100 values of variable2. Hence the warning. Try this instead,

combi$DT[is.na(combi$DT) & !is.na(combi$OD)] <- combi$OD[is.na(combi$DT) & !is.na(combi$OD)] 
like image 29
Sotos Avatar answered Sep 23 '22 17:09

Sotos