Beginner question - factor variables (Tidyverse)

207 Views Asked by At

I think this is a simple question for most but I am new to r and was hoping someone could help.

How can I create a new factor variable for levels of a variable (e.g. 0 = no, 1= yes). Then how do I put this into a new data frame?

So far I have been playing with the 'select' and 'mutate' functions but cannot get somewhere that I can create a correlation plot with the variable.

Thanks in advance! I slammed my laptop shut too many times over this!

1

There are 1 best solutions below

0
On

You can do like this:

library(tidyverse)
df = tibble(
  fct = rep(0:1, each=100) %>% factor(),
  x =rep(1:100, 2), 
  y = rnorm(200)
)

df %>% ggplot(aes(x, y, col=fct))+geom_line()

Read this carefully: https://github.com/rstudio/cheatsheets/raw/master/factors.pdf