我正在尝试将 data.frame 中的 NA 值替换为 0。我知道这是一个非常简单的问题,但由于某种原因它对我不起作用。到目前为止,这是我的代码:
library(XLConnect)
filenames <- list.files( paste(mainDir,sep=""), pattern="Output.*xls", full.names=TRUE)
data = lapply(filenames, function(f) {
wb = loadWorkbook(f)
readWorksheet(wb, sheet = getSheets(wb), startRow = 1, startCol = 1, header=TRUE) })
for (i in 1:length(data)){
data[[i]][is.na(data[[i]])] <- 0}
我的data
包含 6 个数据框,每个看起来像这样:
X North South East West
1 1 1.4 -0.8 NA 0.2
2 2 0.8 0.1 NA NA
3 3 1.1 NA 0.3 NA
4 4 0.7 -0.3 0.5 NA
: : : : : :
: : : : : :
即使我尝试像这样替换单个数据框中的 NA:
x<-data[[1]]
x[is.na(x)]<-0
它也不起作用,但没有错误出现。我已经检查过了str(data)
,我的数据肯定在data.frame
编辑: 的输出dput(head(data))
,数据非常大,所以这些只是前几行,最后几行,但都非常相似
list(structure(list(X.......... = c("01", "02", "03",
"04", "05", "06", "07", "08", "09",
"10", "11", "12"), North = c("NA", "NA", "NA",
"NA", "NA", "NA", "NA", "159268.712943834", "159268.712943834",
"159268.712943834", "NA", "NA"), South = c(0.606714762968571,
0.814522728179517, 0.209726636027901, 0.0444084477658611, -0.374746980093072,
-0.686918667591031, -0.00947578135844365, -0.579281055756145,
-0.447180610635141, 0.0364485438280426, 0.293432135759165, -0.128575801748206
), East = c(0.0453524581429493, -0.715043414690337, -0.726352946071858,
-0.211008344503713, 0.159243426048929, 0.124256257795459, -0.971001351195061,
-1.11413010910649, -0.608926167442848, -1.29473850887024, -1.2685456908235,
-2.19150672218728)
:
:
:
:
.Names = c("X..........", "North", "South", "East", "West"......
:
:
row.names = c(NA, -12L), class = "data.frame"),
structure(list(m = c(0, 0)), .Names = "m", row.names = c(NA,
-2L), class = "data.frame"))
的输出str(data)
,同样有很多数据,但它们都非常相似,所以这里是前几行:
List of 6
$ :'data.frame': 12 obs. of 24 variables:
..$ X..........: chr [1:12] "01" "02" "03" "04" ...
..$ North : chr [1:12] "NA" "NA" "NA" "NA" ...
..$ South : num [1:12] 0.6067 0.8145 0.2097 0.0444 -0.3747 ...
..$ East : num [1:12] 0.0454 -0.715 -0.7264 -0.211 0.1592 ...