R, remove rows with empty strings from data.frame across all columns

dataframe, r, string

Solution

Try

 ladta <- ladta[!apply(ladta, 1, function(x) any(x=="")),] 

Here, `apply` gives each row to `any`, which checks if the expression `x==""` (which is itself a vector) is true for any of the elements and if so, it returns `TRUE`. The whole `apply` expression thus returns a vector of `TRUE/FALSE` statements, which are negated with `!`. This can then be used to subset your data.

Problem

I would like to remove all rows from the data frame where any of the available columns has a string of zero length. I tried making use of the complete cases function but it doesn't work as, presumably some of the strings have empty white spaces. Consequently, I would like to search all columns of the data.frame and remove all rows that have an empty string in one of the available columns. My data frame is defined as ladata. ``` # Remove incomplete cases ladta <- ladta[complete.cases(ladta),] ```

Original source