在两个不同的列上有条件地填充列值

时间:2018-03-23 14:25:36

标签: r tidyr

我搜索过但无法找到,如果已经被问到,请指出我的答案。

我有三列。患者姓名,是否有抗生素,以及抗生素的品牌。然而,有时我会从#34; Antibiotics"但是我在第三栏中注意到他们说他们得了抗生素,因为他们收到的品牌并不算数。

df<-data.frame(Names=c("Patient1","patient2","Patient3","Patient4","patient5"),Antibiotics=c("Y","N","Y","","Y"), TypeAntibiotic=c("","","BrandA",,"BrandA",""))

我想有条件地填补&#34; Antibiotics&#34;如果第三栏有&#34; BrandA&#34;。但是,如果已经有了某些东西,那么我就不想这样做了。抗生素&#34;

我知道:

    df%>%mutate(Antibiotics=ifelse(grepl("BrandA",TypeAntibiotic),"N",Antibiotics))

会给我一个&#34; N&#34;在我想要的专栏中,但我相信它会覆盖我已经拥有的任何&#34; Y&#34; s。我怎么只放一个&#34; N&#34;如果那行中还没有东西?

4 个答案:

答案 0 :(得分:3)

这是基础R解决方案

myindex=df$Antibiotics==""&df$TypeAntibiotic=="BrandA"
df$Antibiotics[myindex]="N"

答案 1 :(得分:1)

我相信这可能就是你要找的东西,

df<-data.frame(Names=c("Patient1","patient2","Patient3","Patient4","patient5"),Antibiotics=c("Y","N","Y","","Y"), TypeAntibiotic=c("","","BrandA","BrandA",""), stringsAsFactors = FALSE)

df$Antibiotics <- ifelse((df$Antibiotics == "") & (df$TypeAntibiotic == "BrandA"),
                         "N", df$Antibiotics

请注意,我稍微修改了df的定义,以便变量不是因子。

如果你想使用你已经拥有的代码,你只需要

df%>%mutate(Antibiotics=ifelse(grepl("BrandA",TypeAntibiotic) & df$Antibiotics == "","N",Antibiotics))

答案 2 :(得分:1)

df$Antibiotics[df$Antibiotics=="" & df$TypeAntibiotic=="BrandA"]<-"N"

哦,我看到@Antonis给了同样的解决方案......

答案 3 :(得分:1)

data.table解决方案

library(data.table)
df<-as.data.table(df)
df[Antibiotics=="" & TypeAntibiotic=="BrandA",Antibiotics:='N']