我有以下数据框
Mor_Rate Hospital Name
15.5 ABC
16 DEF
NA GHI
如果Mor_Rate包含NA,我想将医院名称更新为NA。 对此更简单的解决方案?
谢谢, Pravellika
答案 0 :(得分:2)
你可以尝试
df$Hospital_Name[is.na(df$Mor_Rate) ] <- NA
或
is.na(df$Hospital_Name) <- is.na(df$Mor_Rate)
df
# Mor_Rate Hospital_Name
#1 15.5 ABC
#2 16.0 DEF
#3 NA <NA>
如果是row
df[is.na(df$Mor_Rate),] <- NA
df <- structure(list(Mor_Rate = c(15.5, 16, NA), Hospital_Name = c("ABC",
"DEF", "GHI")), .Names = c("Mor_Rate", "Hospital_Name"), class = "data.frame",
row.names = c(NA, -3L))