0

I have a data frame that includes three columns that have numerical data. I would like to exclude cases where all three columns contain NA values, but not exclude columns where zero, one, or two of the columns contain NA values.

BA_B1_Con.2016 = data.frame(Cm20 = c(0.3, 0.4, 0.3, NA, NA, NA), Cm40 = c(NA, 0.3, 0.3, NA, 0.5, NA), Cm60 = c(0.5, NA, 0.5, NA, 0.4, NA))

I would like the 4th and 6th cases to be excluded since they equal NA values in all three columns, but would like the other NA values to remain in the data frame. I've tried to subset by indicating to exclude cases where all three columns equal NA, but that doesn't quite work right.

I've tried:

BA_B1_Con.2016 <- subset(BA_B1_Con.2016,Cm20 != "NA" & Cm40 != "NA" & Cm60 != "NA")

This doesn't quite work right. I believe I should be able to do this with the subset() function, but am open to other thoughts.

Thanks!

bxcrunner
  • 21
  • 3
  • `BA_B1_Con.2016[rowSums(is.na(BA_B1_Con.2016)) != ncol(BA_B1_Con.2016), ]` – Ronak Shah Aug 23 '19 at 03:26
  • @RonakShah Your answer did not help/was incorrect, but the links to the other answers are helpful. – bxcrunner Aug 23 '19 at 03:42
  • Can you explain why was it not helpful/incorrect? It gives the expected output on the **data you provided** (excluded row 4th and 6th). – Ronak Shah Aug 23 '19 at 03:45
  • It did not remove any rows in my dataframe....but this is probably my fault. My apologies. There are three additional columns in the dataframe containing non-numerical data (site, date, time). Would that need to be accounted for by specifying specific columns in your code? – bxcrunner Aug 23 '19 at 03:49
  • This works: ```BA_B1_Con.2016[!(is.na(BA_B1_Con.2016$Cm20)) | !(is.na(BA_B1_Con.2016$Cm40)) | !(is.na(BA_B1_Con.2016$Cm60)),]``` – bxcrunner Aug 23 '19 at 03:58

0 Answers0