lundi 11 janvier 2021

Avoiding for loop in RStudio

I have written 2 codes below,

# code1
for (i in 1:nrow(Y))
{ 
  if(is.na(Y$b[i])) {} else {
    X <-mutate(X, a = ifelse(col1 == Y$col1[i] & col2 == Y$col2[i],
                                             Y$b[i],a) )
  }
}

and

# code2
if(is.na(Y$b)) {} else {
  X$a <- ifelse(X$col1 == Y$col1 & X$col2 == Y$col2, Y$b, X$a)
}

I have verified few records from code1 and code2 and found no differences. But I am getting warnings when I run code2 as,

Warning messages:
1: In if (is.na(Y$b)) { :
  the condition has length > 1 and only the first element will be used
2: In X$col1 == Y$col1 :
  longer object length is not a multiple of shorter object length
3: In X$col2 == Y$col2 :
  longer object length is not a multiple of shorter object length

Kindly help me if there is no harm in using code2 instead code1, please suggest if any alternate.

Note: X and Y dataframes are having different number of records

Aucun commentaire:

Enregistrer un commentaire