select variables from dataframe based on value

I have a data frame and want to drop variables that having NA only, like sd.
Is there a better way to do so? Thanks in advance!

data <- data.frame(
stringsAsFactors = FALSE,
meastype = c(372, 372, 372, 372, 372, 372, 372, 372, 372, 372),
measid = c(0, 0, 0, 0, 0, 0, 14831, 14832, 14833, 14834),
p90 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
smp = c("N", "N", "N", "N", "N", "N", "N", "N", "N", "N"),
sd = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
)

You did not tell us what you are doing now. Is this of any help?

Note the change name from data to dat1 . data() is an R function.

dat1 <- data.frame(
  stringsAsFactors = FALSE,
  meastype = c(372, 372, 372, 372, 372, 372, 372, 372, 372, 372),
  measid = c(0, 0, 0, 0, 0, 0, 14831, 14832, 14833, 14834),
  p90 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
  smp = c("N", "N", "N", "N", "N", "N", "N", "N", "N", "N"),
  sd = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
)

# Basic As assuming you know the column number
dat1[, -5]

library(tidyverse)
dat1   %>%   select(, -sd)

library(data.table)
dat2 <- as.data.table(dat1) # Covert  to data.table 
dat2[, sd := NULL]

# How to check if you have all NA columns
colSums(!is.na(dat1)) == 0

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.