1

I want to remove rows that contain empty strings in all columns except the first column. I can do this if I know how many columns there are, as I can do this using the following (assuming three columns in tibble):

table %>% filter(.[[2]] != "" & .[[3]] != "")

However, I need code for when I don't know the number of columns...

Ideal solutions will use tidyverse as much as possible :)

Help!

  • You can get the column count for a `dataframe` or `matrix` with the base R `ncol` function: `ncols <- ncol(df)` And pass that to follow on functions. – SteveM Nov 15 '20 at 13:49
  • Does this answer your question? [dplyr filter with condition on multiple columns](https://stackoverflow.com/questions/43938863/dplyr-filter-with-condition-on-multiple-columns) – Alan Nov 15 '20 at 19:20

1 Answers1

0

The subject refers to the nth column but the question and the example in the question refer to the first column so we assume that the first column is meant. Use ncol to find the number of columns if that is not the case.

Using the builtin anscombe data frame and setting some blanks any of the following should work. Note that we have used and defined a rowAny base R function following the colwise vignette that comes with dplyr.

a <- anscombe; a[1, -1] <- ""  # input data   

a %>% filter(rowSums(across(-1, ~ . != "")) > 0)

rowAny <- function(x) rowSums(x) > 0  # see colwise dplyr vignette
a %>% filter(rowAny(across(-1, ~ . != "")))

a %>% rowwise %>% filter(any(c_across(-1) != "")) %>% ungroup

a %>% filter(rowAny(.[-1] != ""))

a[rowAny(a[-1] != ""), ] # base R
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • Thank you very much. These are elegant solutions. The issue I had was keeping the first column in the data set for later pipe processing. You've been a great help! – Luke Wheeler Nov 21 '20 at 06:40