I have a data frame of participant questionnaire responses in wide format, with each column representing a particular question/item.
The data frame looks something like this:
id <- c(1, 2, 3, 4)
Q1 <- c(NA, NA, NA, NA)
Q2 <- c(1, "", 4, 5)
Q3 <- c(NA, 2, 3, 4)
Q4 <- c("", "", 2, 2)
Q5 <- c("", "", "", "")
df <- data.frame(id, Q1, Q2, Q3, Q4, Q5)
I want R to remove columns that has all values in each of its rows that are either (1) NA or (2) blanks. Therefore, I do not want column Q1 (which comprises entirely of NAs) and column Q5 (which comprises entirely of blanks in the form of "").
According to this thread, I am able to use the following to remove columns that comprise entirely of NAs:
df[, !apply(is.na(df), 2, all]
However, that solution does not address blanks (""). As I am doing all of this in a dplyr pipe, could someone also explain how I could incorporate the above code into a dplyr pipe?
At this moment, my dplyr pipe looks like the following:
df <- df %>%
select(relevant columns that I need)
After which, I'm stuck here and am using the brackets [] to subset the non-NA columns.
Thanks! Much appreciated.