Let's say I have the following matrix,
mat.data <- c(1, NA, NA, NA, 1, NA, 1, 2, 3)
mat <- matrix(mat.data,nrow=3,ncol=3,byrow=TRUE)
mat
[,1] [,2] [,3]
[1,] 1 NA NA
[2,] NA 1 NA
[3,] 1 2 3
Where the columns are sequential measurements of 3 different individuals (the rows), in this case, NA could represent either the person being dead OR a missing value. It can be assumed that if an NA value is only followed by NA values then the person is dead, otherwise, the value is missing.
As such, I am looking to create a for loop with if statements to change the NA values to be 100 if it is missing or 99 if the person is dead. Such that we would end up with the following matrix.
mat1.data <- c(1, 99, 99, 100, 1, 99, 1, 2, 3)
mat1 <- matrix(mat1.data,nrow=3,ncol=3,byrow=TRUE)
mat1
[,1] [,2] [,3]
[1,] 1 99 99
[2,] 100 1 99
[3,] 1 2 3
I am having an issue categorising the missing values. I am looking for it to equal 100 if the value mat[r,c] is NA and after it in the row are other non-NA values. This is the code I was starting with but unsure what to do for the part after the &&.
mat1 <- matrix()
for (x in 1:nrow(mat)) {
for (y in 1:ncol(mat)) {
if (is.na(mat[x,y]) && (!is.na(mat[x,y+c(0:(ncol(mat)-y))]))){
mat1[x,y] = 100
}
else if(is.na(mat[x,y])){
mat1[x,y] = 99
}
else
mat1[x,y] = mat[x,y]
}
}