Search code examples
rdplyrmagrittr

how to transform a string into a factor and sets contrasts using dplyr/magrittr piping


i have a rather specific question: how can I make a string into a factor and set its contrasts within a pipe ?

Let's say that I have a tibble like the following

tib <- data_frame (a = rep(c("a","b","c"),3, each = T), val = rnorm(9))

Now, I could use then two separate lines

tib$a <- factor(tib$a)
contrasts(tib$a) <- contr.sum(3)

But what If I wanted to perform the same actions within a pipe from dplyr ?


Solution

  • Ok, this was a fun puzzle, since I never used do() before, but this works for me:

    tib <- data.frame (a = rep(c("a","b","c"),3, each = T), val = rnorm(9)) 
    
    tib = tib %>% mutate(a = factor(a)) %>% do({function(X) {contrasts(X$a) <- contr.sum(3); return(X)}}(.))
    
    contrasts(tib$a)
    

    result:

      [,1] [,2]
    a    1    0
    b    0    1
    c   -1   -1
    

    Hope this helps!

    EDIT: Comment request for explanation, see below:

    This was also new for me. As I understand it, within the do() call, it says

    {func}(.)
    

    this means that a function should be called, with argument ., which is the dataframe in a do call. within func, we then specify the function as

    function(X) {operation to perform on X}
    

    so adding this together:

    {function(X) {operation to perform on X}}(.)
    

    Which means . is used as argument in function X, so it basically becomes 'operation to perform on .'