Search code examples
rif-statementdataframeis-empty

Creating new column in data frame with condition on nonempty cells


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}

Solution

  • 'ifelse' is your friend here. It's vectorized so no need for a loop here.

    df <- data.frame(A = c("aa","aa","aa"), B = c("bb","","bb"))
    df$S <- ifelse(df$B == '', df$A, df$B) 
    
    #   A  B  S
    #1 aa bb bb
    #2 aa    aa
    #3 aa bb bb
    

    If you wanted to adjust your code, this works, but it's less efficient then the ifelse variant:

    df$S = NA
    for(k in 1:nrow(df)) df$S[k] <- if (df$B[k] == "") df$A[k] else df$B[k]
    

    Note the 1:nrow(df) instead of dim(df) and fixed indexing (df$B[k] vs df$BB)