gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 22:30:11 26 4
gpt4 key购买 nike

我有一个数据集列表。

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)

关于r - 如何使用 lapply() 将 NA 更改为 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42894970/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com