문제

Okay, I'm close. Everything works but the last loop for compound where I get hung up on a data type issue. Copy and run to your heart's content.

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

I'm trying to create an indicator variable for null instances and use it to create a compound variable that will zero out the original variable when null and flash the indicator.

도움이 되었습니까?

해결책

Create indicator var's for missing instances and zero out or impute values for NA instances in original data:

# 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 

Copy and run.

다른 팁

Is this what you're looking for ?

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top