Home > OS >  Function using if statement in R
Function using if statement in R

Time:05-22

I want to make a relatively simple function that takes a matrix or data frame and a single number as entries. Then, the function will replace the specific entry of the matrix or data frame with other numbers. My function is

spot_cell_type = function(estimated_cell, cell_type){
  estimated_cell = estimated_cell
  for (i in 1:nrow(estimated_cell)) {
   for (j in 1:ncol(estimated_cell)){
     if (estimated_cell[i,j]==cell_type){
     estimated_cell[i,j]==1
   } else if (estimated_cell[i,j]=NA){
     estimated_cell[i,j]==0
   } else {
     estimated_cell[i,j]==0
     }
   }
  }
  return(estimated_cell)
}

where estimated_cell is a matrix or dataframe form and cell_type is a non-negative integer. Why this function results error "Error in if (estimated_cell[i, j] == cell_type) { : missing value where TRUE/FALSE needed"? How to fix it? This is an example of a data frame

estimated_cell = data.frame(A = c(0,1,2,3,3), B = c(2,1,3,2, 1), C = c(1,3,2,0, NA), D = c(3,1,2,3, NA))
colnames(estimated_cell) = c("0", "1", "2", "3")

CodePudding user response:

Like we discussed in the comments, your function can be defined, but it fails when you apply it to your data because your data has NA, which breaks your if() statement.

You could add an additional conditional to your loop that skips a value if it is NA, like you suggested. Note also that to reassign the value in the original dataframe, you'll want to use (for example) estimated_cell[i,j] <- 0 instead of estimated_cell[i,j]==0.

Or you could use ifelse():

my_func2 <- function(estimated_cell, cell_type){
  as.data.frame(ifelse(estimated_cell == cell_type, 1, 0))
}

my_func2(estimated_cell, 3)
#>   X0 X1 X2 X3
#> 1  0  0  0  1
#> 2  0  0  1  0
#> 3  0  1  0  0
#> 4  1  0  0  1
#> 5  1  0  0 NA
Created on 2022-05-21 by the reprex package (v2.0.1)

CodePudding user response:

I solved the issue like this

spot_cell_type = function(estimated_cell, cell_type){
estimated_cell = estimated_cell
for (i in 1:nrow(estimated_cell)) {
  for (j in 1:ncol(estimated_cell)){
    if (!is.na(estimated_cell[i,j]) & estimated_cell[i,j]==cell_type){
      estimated_cell[i,j]<-cell_type
    } else if (is.na(estimated_cell[i,j])){
      estimated_cell[i,j]<- -1
    } else {
      estimated_cell[i,j]<- -1
    }
  }
}
return(estimated_cell)
}

It works perfect now.

  • Related