Unable to write to Matrix From Loop in R

advertisements

I am trying to write to a matrix via a loop within apply. Specifically I want to sort through the dataset and assign each value in each variable high or low based off of the median for that particular variable. These high lows would be input into the matrix.

My code thus far is:

df I am working with is a dataset with 36 rows and a large number of columns although I am only interested in a selection of them for this case.

rows <- 36
columns <- 100

output <- matrix(ncol = columns, nrow = rows)

apply(df[,463:500],2, function(x)
    for (i in 1:36){
    if (x[i] <= median(x)){
        output[i,x-462] <- "low"
    }
    else
        output[i,x-462] <- "high"
    })

First is when there are NAs in my code I am getting the error: Error in if (x[i] <= median(x)) { : missing value where TRUE/FALSE needed

and I don't know how make R ignore the NAs.

The second problems is that even when I use only columns in df without NAs, the code just returns null and the matrix is still empty

I know that simply writing to the matrix works, for example:

> output[1,1] <- "low"
> output[1,1]
[1] "low"

this feels like it should be really simple, any ideas?

Thanks


So I ended up getting it to work with this code:

rows <- 36
columns = 50

output <- matrix(ncol = columns, nrow = rows)

for (i in 463:482){
  for (j in 1:36){
      if (is.na(df[j,i]) == FALSE){
          if (df[j,i] <= median(df[,i])){
             output[j,i-462] <- "low"
        }
          else
             output[j,i-462] <- "high"
    }
  }
}

I did away with apply. I will admit that I am much more familiar with basic looping and almost not at all with apply lapply so this made more sense to me.

However this still fails to deal with NA and always got the error

missing value where TRUE/FALSE needed

Is it possible to modify my code to compensate for all NA entries?