为大型数据框创建和命名NA指标变量

时间:2014-02-18 20:03:19

标签: r indicator

好的,我很接近。一切正常,但compound的最后一个循环,我挂在数据类型问题上。复制并运行您的内心。

x <- c(1:12)
dim(x) <- c(3,4)
x[2,2] <- NA
x[3,3] <- NA
colnames(x) <- c("A","B","C","D")

x

newframe <- data.frame(matrix(0, ncol = 4, nrow = 3))

for (i in 1:3)
  for (j in 1:4)
  { newframe[i,j] <-  (1 -1*(is.na(x[i,j]))) }

newframe <- as.matrix((newframe))

newframe

compound <- data.frame(matrix(0, ncol = 4, nrow = 3))

for (i in 1:3) 
  for (j in 1:4 )
  {  compound[i,j] <- (as.numeric(x[i,j])*(as.numeric(newframe[i,j])))
}

compound

我正在尝试为null实例创建一个指示符变量,并使用它来创建一个复合变量,当null为空时将原始变量清零并闪烁指示符。

2 个答案:

答案 0 :(得分:1)

为原始数据中的NA实例创建缺失实例的指示符var和零值或插值:

# create data
x <- c(1:12)
dim(x) <- c(3,4)
x[2,2] <- NA
x[3,3] <- NA

x

# create data frame for indicator var's
newframe <- 1*(is.na(x))

newframe
class(newframe)

# zero out NAs in data, or alternatively replaced with imputed values
x[is.na(x)] <- 0

# create data frame for original data and indicator var's
newdata <- cbind(x, newframe)

newdata 

复制并运行。

答案 1 :(得分:0)

这是你要找的吗?

compound <- x
compound[is.na(x)] <- 0
compound
     A B C  D
[1,] 1 4 7 10
[2,] 2 0 8 11
[3,] 3 6 0 12