Home > front end >  Creating a new variable based on levels of several other variables in R
Creating a new variable based on levels of several other variables in R

Time:02-03

I´m not very experienced in programming. I´m trying to create a new variable which is comprised of factor levels of 7 existing variables: Here´s an example:

variables=data.frame(var1=c(0,1,2,3), var2=c(0,1,1,0), var3=c(1,0,1,1), var4=c(001,110,211,301))

So what I want to create is var4, which is a compound of all levels in each row of var1, var2 and var3. Are there any suggestions for an elegant solution? Thanks a lot in advance!

CodePudding user response:

You could use unite to combine the columns and use bind_cols to add them later to your dataframe like this:

variables=data.frame(var1=c(0,1,2,3), var2=c(0,1,1,0), var3=c(1,0,1,1))
library(dplyr)
library(tidyr)

variables %>% 
  bind_cols(
    variables %>%
      unite("var4", var1:var3, sep = ""))
#>   var1 var2 var3 var4
#> 1    0    0    1  001
#> 2    1    1    0  110
#> 3    2    1    1  211
#> 4    3    0    1  301

Created on 2023-02-03 with reprex v2.0.2

CodePudding user response:

The built-in function for this is interaction():

transform(variables, var4 = interaction(var1, var2, var3, sep = "", drop = TRUE, lex.order = TRUE))

  var1 var2 var3 var4
1    0    0    1  001
2    1    1    0  110
3    2    1    1  211
4    3    0    1  301
  • Related