na
Combine column to remove NA’s
A dplyr::coalesce based solution could be as: data %>% mutate(mycol = coalesce(x,y,z)) %>% select(a, mycol) # a mycol # 1 A 1 # 2 B 2 # 3 C 3 # 4 D 4 # 5 E 5 Data data <- data.frame(‘a’ = c(‘A’,’B’,’C’,’D’,’E’), ‘x’ = c(1,2,NA,NA,NA), ‘y’ = c(NA,NA,3,NA,NA), ‘z’ = c(NA,NA,NA,4,5))
Subsetting R data frame results in mysterious NA rows
Wrap the condition in which: df[which(df$number1 < df$number2), ] How it works: It returns the row numbers where the condition matches (where the condition is TRUE) and subsets the data frame on those rows accordingly. Say that: which(df$number1 < df$number2) returns row numbers 1, 2, 3, 4 and 5. As such, writing: df[which(df$number1 < df$number2), … Read more
Fastest way to detect if vector has at least 1 NA?
As of R 3.1.0 anyNA() is the way to do this. On atomic vectors this will stop after the first NA instead of going through the entire vector as would be the case with any(is.na()). Additionally, this avoids creating an intermediate logical vector with is.na that is immediately discarded. Borrowing Joran’s example: x <- y … Read more