如何使用 lapply() 将 NA 更改为 0

  • 本文关键字:NA 何使用 lapply r
  • 更新时间 :
  • 英文 :


>我有一个数据集列表。

dfList <- list(df1,df2,df3)

每个数据集如下所示。

apples, oranges
1,      2
NA,     4

我想以编程方式将每个数据帧的NA更改为0 s。我该怎么做?

到目前为止我的代码...

lapply(
  X = dfList,
  FUN = cbind,
  is.na = FALSE
)

我们可以使用replace

dfList1 <- lapply(dfList, function(x) replace(x, is.na(x), 0))
dfList1
#[[1]]
#  apples oranges
#1      1       2
#2      0       4
#[[2]]
#  apples oranges
#1      1       2
#2      0       4
#[[3]]
#  apples oranges
#1      1       2
#2      0       4

数据

df2 <- df1
df3 <- df1
dfList1 <- list(df1, df2, df3)

最新更新