mercredi 24 juin 2020

Code Review for simple column summary function in R

I'm working through R for Data Science and the question I'm working on is asking to adapt an already made function to only numeric columns.

The original function is the following:

   out <- vector("double", length(df))
   for (i in seq_along(df)) {
        out[i] <- fun(df[[i]])
       
        out[i] = "Not Numeric"
     }
   out
} 

The adapted function is the following:

    out <- vector("double", length(df))
    for (i in seq_along(df)) {
        if (is.numeric(df[[i]] == TRUE)){
        out[i] <- fun(df[[i]])
        } else{
            out[i] = "Not Numeric"
        }
    }
    out
}

And I was doing the testing on the following data frame:

    a = rnorm(10),
    b = rnorm(10),
    c = rnorm(10),
    d = letters[1:10],
    e = LETTERS[11:20],
    f = runif(10)
)

So the way the function is supposed to work is that say I wanted to calculate the means of the columns. Then I would proceed to enter: col_summary(df,mean) and the output should be: mean_1 mean_2 mean_3 "Not Numeric" "Not Numeric" mean_4

Instead I'm just getting a vector of "Not Numerics". So the question is what did I do wrong in my attempt. I don't see anything wrong, but I guess that's why I'm here now.

Aucun commentaire:

Enregistrer un commentaire