replace na with string in data frame code example
Example 1: replace any NA in a data frame in r
a = data.frame(a=c(NA,1,2,NA), b=c(1,1,2,NA),c=c(NA,NA,2,NA))
for (i in 1:3){
a[which(is.na(a[,i])),i] = 0
}
Example 2: replace na in a column with values from another df
library(dplyr)
df1 %>% inner_join(df2, by= "ID") %>%
mutate(Count = coalesce(Count.x, Count.y)) %>%
select(ID, Count)
# ID Count
# 1 11 345
# 2 22 456
# 3 33 786
# 4 44 765
# 5 55 890
# 6 66 888
# 7 77 654