Package: expss


Function: drop_empty_columns()


1. Remove empty columns

Review the data (d4)

# A tibble: 5 x 5
  extra1 extra2 extra3 stu_id test_score
  <chr>  <lgl>   <dbl>  <dbl>      <dbl>
1 a      NA          2     10        205
2 b      NA          0     11        220
3 c      NA       -999     12        250
4 d      NA          0     13        217
5 e      NA         NA     NA         NA

Count the number of columns in the current data using the function base::ncol()

ncol(d4)
[1] 5

Filter out any completely empty column

d4 <- d4 %>% expss::drop_empty_columns()

d4
# A tibble: 5 x 4
  extra1 extra3 stu_id test_score
  <chr>   <dbl>  <dbl>      <dbl>
1 a           2     10        205
2 b           0     11        220
3 c        -999     12        250
4 d           0     13        217
5 e          NA     NA         NA

Count the number of cases after you filter

ncol(d4)
[1] 4

Package: janitor


Function: remove_empty()


1. Remove empty columns

Review the data (d4)

# A tibble: 5 x 5
  extra1 extra2 extra3 stu_id test_score
  <chr>  <lgl>   <dbl>  <dbl>      <dbl>
1 a      NA          2     10        205
2 b      NA          0     11        220
3 c      NA       -999     12        250
4 d      NA          0     13        217
5 e      NA         NA     NA         NA

Count the number of columns in the current data using the function base::ncol()

ncol(d4)
[1] 5

Remove any completely empty column

  • Note: We add the argument which and in this case we select “cols” rather than the other option “rows”.
d4 <- d4 %>% janitor::remove_empty(which = "cols")

d4
# A tibble: 5 x 4
  extra1 extra3 stu_id test_score
  <chr>   <dbl>  <dbl>      <dbl>
1 a           2     10        205
2 b           0     11        220
3 c        -999     12        250
4 d           0     13        217
5 e          NA     NA         NA

Count the number of cases after you filter

ncol(d4)
[1] 4

Return to Select Variables