I have data that looks like the following:
pig<-data.frame(a=c("","","","","","Type1"),b=c("T1 NR","T2 NR","T2","T3","T3","Type1"))
print(pig)
a b
T1 NR
T2 NR
T2
T3
T3
Type1 Type 1
Where a will sometimes be blank, but b always has information that corresponds to the missing information in a. I am looking for two things. First, where "NR" is present in b, I want "NR" to appear in a. Second, where "NR" is not present in b and a is blank, I would like some other string to appear in such as "DKT" to get something like the following:
cow<-data.frame(a=c("NR","NR","DKT","DKT","DKT","Type1"),b=c("T1 NR","T2 NR","T2","T3","T3","Type1"))
print(cow)
a b
NR T1 NR
NR T2 NR
DKT T2
DKT T3
DKT T3
Type1 Type1
Thanks!
library(tidyverse);pig %>% mutate(a = case_when(str_detect(b, "NR") ~"NR", a == "" ~ "DKT", TRUE ~ as.character(a)))ifelse(pig$a == "", ifelse(grepl("NR", pig$b), "NR", "DKT"), as.character(pig$a))