Hello guys ! It might seems to be really easy to you but i'm struggling to find a way to replace every NA of a column with the corresponding value in another column. _ Q1 _ Q2 1 23 NA 2 46 65 3 34 NA 4 67 54 ... In this exemple i'd like to replace NA from ligne one with 23, and NA from ligne 3 with 34.
Thank you in advance !
Happy new year to you
Happy new year.
dat <- data.frame( Q1 = c(23, 46, 34, 67), Q2 = c(NA, 65, NA, 54)) the_na <- which(is.na(dat[2])) dat[the_na,2] <- dat[the_na,1] dat #> Q1 Q2 #> 1 23 23 #> 2 46 65 #> 3 34 34 #> 4 67 54
Or in Tidyverse and using coalesce
dat <- dat %>% mutate(Q2 = coalesce(Q2,Q1))
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.