我正在尝试将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 ...
答案 0 :(得分:0)
问题在于您的数据。有些NA列被编码为字符。 is.na函数无法识别“NA”。请参阅以下示例:
is.na(c(2,3,5,"NA"))
# FALSE FALSE FALSE FALSE
同时以下代码可以满足您的需求。
is.na(c(2,3,5,NA))
# FALSE FALSE FALSE TRUE
只需用NA替换“NA”,您的代码就可以正常工作。