Sagar
Sagar

Reputation: 191

R function ifelse returning only evaluation of the last row

I have a 200 x 10 matrix of digits , either 2 or 7 called predictions.

I'm trying to return the majority for each row using this code.

for (i in 1:nrow(predictions)) {
  if_else(mean(predictions[i,] == 7) > 0.5, 7, 2)
}

This returns nothing at all on the console.

If I try to assign a variable to it like so:

for (i in 1:nrow(predictions)) {
  if_else(mean(predictions[i,] == 7) > 0.5, 7, 2)
} -> ens

it returns the result for the last row.

If I try to assign a variable to it at the beginning, the variable contains NULL:

ens <- for(i in 1:nrow(predictions)) {
  if_else(mean(predictions[i,] == 7) > 0.5, 7, 2)
}

What am I missing?

Upvotes: 0

Views: 90

Answers (3)

SteveM
SteveM

Reputation: 2301

You generally do not want or need to process data frame content with for loops. You can generate the row means as a vector with the rowMeans function. Try:

result <- ifelse(rowMeans(predictions) > 0.5, 7, 2)

Upvotes: 1

latlio
latlio

Reputation: 1587

Your previous code is not returning anything because you're not saving the results of each iteration. To return the majority for each row, the minimum number of modifications to achieve what you want should be like this:

majority <- c()
for(i in 1:nrow(predictions)){
  majority[i] <- if_else(mean(predictions[i,]==7)>0.5,7,2) 
}

Then, depending on what you wanted to do with the vector of majorities, you could either mutate a row number or bind it to the original predictions matrix.

EDIT

If you want to step away from using for loops, you can use apply statements. If you want to stay in the tidyverse (I see you're using dplyr::if_else()), check out the purrr family of map() functions.

Upvotes: 3

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521073

I think you want to be using apply here in row mode:

ens <- apply(predictions, 1, function(x) {
    if (mean(x == 7) > 0.5) 7 else 2
})

Note that I am using regular if ... else non vectorized flow logic, since we are dealing with scalar aggregates of each row in the anonymous function being passed to apply().

Upvotes: 2

Related Questions