lundi 26 février 2018

creating new column in data frame with condition on nonempty cells in R

I have a table looking like this:

  A   B
  aa  bb
  aa  
  aa  bb

And I want to Check if a data frame cell is blank and if yes find a result table like this:

  A  B  S
  aa bb bb
  aa    aa
  aa bb bb

I'm using this code but it doesn't work

for(k in dim(df))
  if (df$BB == ""){
    df$S <- df$AA
  }else {df$S <- df$BB}

Aucun commentaire:

Enregistrer un commentaire