Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

dplyr

magrittr

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 ?

like image 284
Federico Nemmi Avatar asked Jan 03 '23 17:01

Federico Nemmi


2 Answers

Everything in R is a function. You just have to know what it's called. In this case, it's contrasts<- to assign contrasts to a factor.

mutate(tib, a=`contrasts<-`(factor(a), , contr.sum(3)))
like image 67
Hong Ooi Avatar answered Jan 13 '23 16:01

Hong Ooi


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 .'

like image 45
Florian Avatar answered Jan 13 '23 16:01

Florian