Search code examples
rfor-loopapplylapplysapply

apply a function to each element of a column of a dataframe


I'm new with R and i have a problem, I have a data frame, and I want to apply this function to each element of column G3

fun_pass <- function(calif){
  if(calif >= 10){
    x <- 1
  }else{
    x <-0
  }
  return(x)
}

The problem is that the function only applies to the first element of the mat_data$G3 column and fills the new mat_data$pass column with that single value.

I use apply for this:

mat_data$pass <- apply(mat_data$G3,2,fun_pass(mat_data$G3))

The goal is to have a new column that tells me if the student passed the course or not.


Solution

  • The issue is with if/else which is not vectorized. If we change the function to ifelse, it would work. Another issue is that apply with MARGIN it expects a data.frame/matrix. Here, it is extracting a vector 'G3'

    fun_pass <- function(calif) ifelse(calif >= 10, 1, 0)
    

    Here we don't need ifelse also

    fun_pass <- function(calif) as.integer(calif >= 10)
    

    If it is a single column, use

    mat_data$pass <- fun_pass(mat_data$G3)