factor function with contrast

I have a tibble (tbl) with a column called Target which contains 2 factors: "yes" and "no". I'd like to set contrast, and then somehow use that to set the factors as 2 levels. I have the following so far:

contrasts(as.factor(tbl$Target))
tbl$Target = factor()  

I know that inside the factor function, I need the data frame and the levels, but I don't know the proper syntax. Can someone help pls? Thanks!

I'm not sure what you're asking here. Do you want to change the character column of yes and no into a factor column of yes and no?

Also, are you running a statistical test?

Does the factor documentation help here? https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/factor

thanks everyone for the inputs. I will try these out.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

"ye" and "no" are already factors. I think the idea is to change them to "0" and "1" based on the return of contrasts. Does the second line of my script do that?

Hi @bobby, I'm not entirely sure what you're after, but the below might be helpful.

There are many contrast functions, depending on what you are looking for. hopefully the below provides a few examples of what you are looking for:

library(tidyverse)

my_factors <- tibble(
  my_factor_chr = sample(c("yes", "no"), 10, replace = T)
) %>%
  mutate(
    my_factor_fct = as_factor(my_factor_chr),
    my_factor_int = as.integer(my_factor_fct)
  )

my_factors
#> # A tibble: 10 x 3
#>    my_factor_chr my_factor_fct my_factor_int
#>    <chr>         <fct>                 <int>
#>  1 yes           yes                       1
#>  2 yes           yes                       1
#>  3 no            no                        2
#>  4 no            no                        2
#>  5 no            no                        2
#>  6 yes           yes                       1
#>  7 no            no                        2
#>  8 yes           yes                       1
#>  9 yes           yes                       1
#> 10 yes           yes                       1


contrasts(my_factors$my_factor_fct)
#>     no
#> yes  0
#> no   1
contr.treatment(my_factors$my_factor_fct)
#>     yes no no no yes no yes yes yes
#> yes   0  0  0  0   0  0   0   0   0
#> yes   1  0  0  0   0  0   0   0   0
#> no    0  1  0  0   0  0   0   0   0
#> no    0  0  1  0   0  0   0   0   0
#> no    0  0  0  1   0  0   0   0   0
#> yes   0  0  0  0   1  0   0   0   0
#> no    0  0  0  0   0  1   0   0   0
#> yes   0  0  0  0   0  0   1   0   0
#> yes   0  0  0  0   0  0   0   1   0
#> yes   0  0  0  0   0  0   0   0   1
contr.sum(my_factors$my_factor_fct)
#>     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
#> yes    1    0    0    0    0    0    0    0    0
#> yes    0    1    0    0    0    0    0    0    0
#> no     0    0    1    0    0    0    0    0    0
#> no     0    0    0    1    0    0    0    0    0
#> no     0    0    0    0    1    0    0    0    0
#> yes    0    0    0    0    0    1    0    0    0
#> no     0    0    0    0    0    0    1    0    0
#> yes    0    0    0    0    0    0    0    1    0
#> yes    0    0    0    0    0    0    0    0    1
#> yes   -1   -1   -1   -1   -1   -1   -1   -1   -1

stat_funs <- lsf.str("package:stats")

stat_funs[str_detect(stat_funs,"contr")]
#>  [1] "contr.helmert"   "contr.poly"      "contr.SAS"       "contr.sum"      
#>  [5] "contr.treatment" "contrasts"       "contrasts<-"     "glm.control"    
#>  [9] "loess.control"   "nls.control"     "se.contrast"

Created on 2020-07-21 by the reprex package (v0.3.0)