我有一个数据集:
Date c00 c01 c02 c03
1 3 4 10 12
2 2 5 NULL 8
3 NULL NULL 20 13
文件名是capacity.intensity,我试图用以下代码将NULL替换为0:
for(i in capacity.intensity [1:3,])
{
for(j in capacity.intensity [,2:5])
{capacity.intensity [i,j]<-
ifelse(as.character(unlist(capacity.intensity [i,j])) == "NULL", "0",
as.character(unlist(capacity.intensity [i,j])))
}
}
但我收到了错误
摘要中的错误(c(32L,32L,32L,32L,32L,32L,32L,32L,32L,: min对因素无意义
当我追溯它时。
请告诉我如何解决这个问题?
答案 0 :(得分:0)
最简单的方法是使用revalue
中的plyr
:
sapply(capacity.intensity,
function(x)revalue(factor(x),c(NULL=0)))
Date c00 c01 c02 c03
[1,] "1" "3" "4" "10" "12"
[2,] "2" "2" "5" "0" "8"
[3,] "3" "0" "0" "20" "13"
其中:
capacity.intensity <-
read.table(text='Date c00 c01 c02 c03
1 3 4 10 12
2 2 5 NULL 8
3 NULL NULL 20 13',header=TRUE)
capacity.intensity是混合整数和因子的数据框架。
答案 1 :(得分:0)
如果您正在处理&#34; NULL&#34;要将其更改为零的因子级别,您可以执行
> as.data.frame(lapply(df, function(x) {
x <- as.character(x)
x[x == "NULL"] <- 0; x
}))
# Date c00 c01 c02 c03
# 1 1 3 4 10 12
# 2 2 2 5 0 8
# 3 3 0 0 20 13
其中df
是数据。或者你可以编写一些你可以保留的功能,稍后再使用。
> foo <- function(x, from, to) {
x <- as.character(x)
x[x == from] <- to
x
}
> as.data.frame(lapply(df, foo, "NULL", 0))
# Date c00 c01 c02 c03
# 1 1 3 4 10 12
# 2 2 2 5 0 8
# 3 3 0 0 20 13