Hi, I have this simple data frame:
source <- data.frame(
stringsAsFactors = FALSE,
URN = c("aa", "bb", "cc", "dd", "ee"),
Model = c("aaa", "bbb", "ccc", "ddd", "eee"),
FamilyName = c(NA, "bbbb", NA, "dddd", "eeee")
)
Is it possible to copy values from 'Model' to 'FamilyName' if 'FamilyName' is blank?
Use ifelse
and is.na()
for the test
You can also use coalesce()
library(dplyr)
source <- data.frame(
stringsAsFactors = FALSE,
URN = c("aa", "bb", "cc", "dd", "ee"),
Model = c("aaa", "bbb", "ccc", "ddd", "eee"),
FamilyName = c(NA, "bbbb", NA, "dddd", "eeee")
)
source %>%
mutate(FamilyName = coalesce(FamilyName, Model))
#> URN Model FamilyName
#> 1 aa aaa aaa
#> 2 bb bbb bbbb
#> 3 cc ccc ccc
#> 4 dd ddd dddd
#> 5 ee eee eeee
Created on 2022-07-06 by the reprex package (v2.0.1)
2 Likes
system
Closed
4
This topic was automatically closed 7 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.