dat <- data.frame(
dog = c(NA, 22, NA, 11),
cat = c(NA, 33, NA, 33),
monkey = c(NA, 33, NA, 33),
rabbit = c(NA, 3838, NA, 3838),
angel = c(NA, 8484, NA, 8484))
dat
#> dog cat monkey rabbit angel
#> 1 NA NA NA NA NA
#> 2 22 33 33 3838 8484
#> 3 NA NA NA NA NA
#> 4 11 33 33 3838 8484
dat[which(is.na(rowMeans(dat))),] <- NA
dat
#> dog cat monkey rabbit angel
#> 1 NA NA NA NA NA
#> 2 22 33 33 3838 8484
#> 3 NA NA NA NA NA
#> 4 11 33 33 3838 8484
dog cat monkey rabbit angel
NA 33 33 3838 8484
88 33 23 NA 2344
38 NA 3838 NA NA
if there is a "NA" in Dog, Monkey, or Cat, there needs to be an NA in all 3. Is there any easy way to do this in tidy verse? thank you!
it doesnt matter if there is NA in Rabbit or Angel, but if there is NA in any one of Dog, Cat, Monkey (for each row).... then the values of Dog, Cat, Monkey in that row all must be 0
rowMeans() takes the mean value of each row without removing NA values. So, if a row contains one or more NA values, rowMeans() will evaluate to NA, and is.na() will evaluate to TRUE. Then which() returns the rows which evaluate to TRUE and [] subsets the dataframe to include only those rows, which <- then assigns to NA.
Whether to use {base} or a {tidyverse} approach is preferable is a matter of individual taste. My preference is {base} because less syntax is required.
Missed the angels, since I'm not a Hallmarks believer.
dat <- data.frame(
dog = c(NA, 22, NA, 11),
cat = c(12, 33, NA, 33),
monkey = c(12, 33, NA, 33),
rabbit = c(15, NA, NA, 3838),
angel = c(NA, 8484,15, 8484))
dat
#> dog cat monkey rabbit angel
#> 1 NA 12 12 15 NA
#> 2 22 33 33 NA 8484
#> 3 NA NA NA NA 15
#> 4 11 33 33 3838 8484
dat[1:4][which(is.na(rowMeans(dat[1:4]))),] <- NA
dat
#> dog cat monkey rabbit angel
#> 1 NA NA NA NA NA
#> 2 NA NA NA NA 8484
#> 3 NA NA NA NA 15
#> 4 11 33 33 3838 8484