jeudi 14 juillet 2016

'object length is not a multiple of shorter object length' after ifelse

I've gone through the other Q&As for this error but still unable to resolve my issue.

I have two data frames which look something like:

partID <- c('partA', 'partB', 'partC')
model <- c('model1', 'model1', 'model2')
dfOne <- data.frame(partID, model)
partID <- c('partC', 'partA', 'partA', 'partB', 'partC', 'partX')
dfTwo <- data.frame(partID)
dfTwo$model <- 'diffModel'

I am trying to replace the models in dfTwo with those in dfOne matched by partID. I have tried doing this with ifelse:

dfTwo$model <- ifelse(dfTwo$partID == dfOne$partID, 
                  dfOne$model, dfTwo$model)

However, I get the error:

"Warning messages: 1: In is.na(e1) | is.na(e2) : longer object length is not a multiple of shorter object length 2: In ==.default(dfTwo$partID, dfOne$partID) : longer object length is not a multiple of shorter object length"

I thought it might be because model in dfTwo was a character and model in dfOne was a factor but I still get the same after I run:

dfTwo$model <- as.factor(dfTwo$model)

I assume its because the number of observations differ but I'm not quite sure how else to go about this where I don't overwrite those models which don't have a partID match.

I'm still fairly new to using R.

Any help would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire