0
for(i in 2:nrow(df1)){
    if(df1[i, 'a'] == '') {
      df1[i, 'a'] <- df1[i - 1, 'a'] 
    }
    
    if(df1[i, 'b'] == '') {
      df1[i, 'b'] <- df1[i - 1, 'b']
    }
    
    if(df1[i, 'c'] == '') {
      df1[i, 'c'] <- df1[i - 1, 'c']
    }
    
    if(df1[i, 'd'] == '') {
      df1[i, 'd'] <- df1[i - 1, 'd']
    }
    
    if(df1[i, 'e'] == '') {
      df1[i, 'e'] <- df1[i - 1, 'e']
    }
    
    if(df1[i, 'f'] == '') {
      df1[i, 'f'] <- df1[i - 1, 'f']
    }
    
    if(df1[i, 'g'] == '') {
      df1[i, 'g'] <- df1[i - 1, 'g']
    }
    
    if(df1[i, 'h'] == '') {
      df1[i, 'h'] <- df1[i - 1, 'h']
    }
    
    if(df1[i, 'j'] == '') {
      df1[i, 'j'] <- df1[i - 1, 'j']
    }
    
    if(df1[i, 'k'] == '') {
      df1[i, 'k'] <- df1[i - 1, 'k']
    }
        
    if(df1[i, 'l'] == '') {
      df1[i, 'l'] <- df1[i - 1, 'l']
    }
    
  }
  return(df1)

Hello, I am trying to use fifelse instead of if loops, but I am confused on how to utilize fifelse. Any help will be appreciated, thank you! I am wanting to remove blank spaces with previous element in the dataframe.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
rivas
  • 35
  • 4
  • Please add a little bit of sample input (in copy/pasteable R syntax, hopefully! e.g. `dput(your_data[1:10, 1:5])` for the first 10 rows and first 5 columns) and the desired output. – Gregor Thomas Jul 25 '22 at 19:58
  • As a best guess, you could replace blanks with `NA`s, `df1[df1 == ""] <- NA` and then use `tidyr::fill` to fill all NA values with the previous observation, `df1 <- tidyr::fill(df1, everything())`. But without any sample data it's hard to tell if that's what you want. – Gregor Thomas Jul 25 '22 at 20:01
  • Gregor, the tidyr::fill function work, thank you for your help! – rivas Jul 27 '22 at 15:51

0 Answers0