将NA替换为特定于行和列的值

时间:2017-03-20 13:40:28

标签: r loops replace data.table na

这个问题中有很多内容。首先,我想按列c对数据进行分段。子集由因子c给出:等级为1到4.因此有4个不同的段。 接下来我有两列。 a和b列。 我想用每个段特定列的最大值替换NA。因此,例如,第3行和第39列的NA,这将是30.(b,3)将是80,(b,8)将是50,(a,5)将是80

我已经创建了下面的代码来完成这项工作,但现在我需要为所有段和列自动(如for循环)。我怎么能这样做?

a <- c(10,NA,30,40,NA,60,70,80,90,90,80,90,10,40)
b <- c(80,70,NA,50,40,30,20,NA,0,0,10,69, 40, 90)
c <- c(1,1,1,2,2,2,2,2,3,3,3,4,4,4)


      a  b c
 1:  10 80 1
 2:  NA 70 1
 3:  30 NA 1
 4:  40 50 2
 5:  NA 40 2
 6:  60 30 2
 7:  70 20 2
 8:  80 NA 2
 9:  90  0 3
 10: 90  0 3
 11: 80 10 3
 12: 90 69 4
 13: 10 40 4
 14: 40 90 4


mytable <- data.table(a,b,c)

mytable[which(is.na(mytable[c == 1][,1, with = FALSE]) == TRUE),1] <- max(mytable[c==1,1], na.rm = TRUE)

不幸的是,这次尝试会导致错误:

for(i in unique(mytable$c)){
  for(j in unique(c(1:2))){
    mytable[which(is.na(mytable[c == i][,j, with = FALSE]) == TRUE),j, with = FALSE] <- max(mytable[c==i][,j, with = FALSE], na.rm = TRUE)
  }
}

[<-.data.table*tmp*中的错误,其中(is.na(mytable [c == i] [,j,with = FALSE])==:   unused参数(使用= FALSE)

令人惊讶的是,这也会导致错误:

for(i in unique(mytable$c)){
  for(j in unique(c(1:2))){
    mytable[which(is.na(mytable[c == i][,j]) == TRUE),j] <- max(mytable[c==i,j], na.rm = TRUE)
  }
}

[.data.table中的错误(mytable,c == i,j):       j([...]内的第二个参数)是单个符号,但是列名称&#39; j&#39;找不到。也许你打算DT [,.. j]或DT [,j,= FALSE]。这种与data.frame的差异是经过深思熟虑的,并在FAQ 1.1中进行了解释。

3 个答案:

答案 0 :(得分:4)

library("data.table")

mytable <- data.table(
a=c(10,NA,30,40,NA,60,70,80,90,90,80,90,10,40),
b=c(80,70,NA,50,40,30,20,NA,0,0,10,69, 40, 90),
c=c(1,1,1,2,2,2,2,2,3,3,3,4,4,4))

foo <- function(x) { x[is.na(x)] <- max(x, na.rm=TRUE); x }

mytable[, .(A=foo(a), B=foo(b)), by=c]

结果:

> mytable[, .(A=foo(a), B=foo(b)), by=c]
#    c  A  B
# 1: 1 10 80
# 2: 1 30 70
# 3: 1 30 80
# 4: 2 40 50
# 5: 2 80 40
# 6: 2 60 30
# 7: 2 70 20
# 8: 2 80 50
# 9: 3 90  0
#10: 3 90  0
#11: 3 80 10
#12: 4 90 69
#13: 4 10 40
#14: 4 40 90

或直接替换ab

mytable[, `:=`(a=foo(a), b=foo(b)), by=c] # or
mytable[, c("a", "b") := (lapply(.SD, foo)), by = c]  # from @Sotos

或更安全的变体(tnx to @Frank for the remark):

cols <- c("a", "b")
mytable[, (cols) := lapply(.SD, foo), by=c, .SDcols=cols]

答案 1 :(得分:2)

使用data.table

library(data.table)
mytable[, a := ifelse(is.na(a), max(a, na.rm = TRUE), a), by = c]
mytable[, b := ifelse(is.na(b), max(b, na.rm = TRUE), b), by = c]

或在一个命令中

mytable[, c("a", "b") := lapply(.SD, function(x) ifelse(is.na(x), max(x, na.rm = TRUE), x)), .SDcols = c("a", "b"), by = c]

答案 2 :(得分:0)

使用包ddply()中的plyr

df<-data.frame(a,b,c=as.factor(c))
library(plyr)
df2<-ddply(df, .(c), transform, a=ifelse(is.na(a), max(a, na.rm=T),a), 
           b=ifelse(is.na(b), max(b, na.rm=T),b))