且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将R中的所选列中的所有NA替换为FALSE

更新时间:2023-02-18 11:55:46

如果你想做更换对于变量的一个子集,您仍然可以使用 is.na(*)< - 技巧,如下所示:

If you want to do the replacement for a subset of variables, you can still use the is.na(*) <- trick, as follows:

df[c("x1", "x2")][is.na(df[c("x1", "x2")])] <- FALSE

使用临时变量的IMO使逻辑更容易遵循:

IMO using temporary variables makes the logic easier to follow:

vars.to.replace <- c("x1", "x2")
df2 <- df[vars.to.replace]
df2[is.na(df2)] <- FALSE
df[vars.to.replace] <- df2