Replace missing values (NA) with blank (empty string)

Another alternative: df <- sapply(df, as.character) # since your values are `factor` df[is.na(df)] <- 0 If you want blanks instead of zeroes > df <- sapply(df, as.character) > df[is.na(df)] <- ” ” > df class Year1 Year2 Year3 Year4 Year5 [1,] “classA” “A” “A” “A” “A” “A” [2,] ” ” ” ” ” ” ” … Read more

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))

aggregate methods treat missing values (NA) differently

Good question, but in my opinion, this shouldn’t have caused a major debugging headache because it is documented quite clearly in multiple places in the manual page for aggregate. First, in the usage section: ## S3 method for class ‘formula’ aggregate(formula, data, FUN, …, subset, na.action = na.omit) Later, in the description: na.action: a function … Read more