How to create a new filed from 3 other dichotomous fields ?

I have 3 fields/parameters : current smoker (0/1), former smoker (0/1), never smoked (0,1)
need to create a new single parameter of 3 levels: (0- never smoked, 1 - former smoker, 2-current smoker).
How should I do this please?
Thank you

Welcome to the community!

You can use max.col, like below. I've subtracted 1 to get the levels as 0, 1 or 2, otherwise you'd get 1, 2 or 3.

set.seed(seed = 35071)

fake_dataset <- t(x = replicate(n = 20, expr = sample(x = c(0, 0, 1))))
colnames(x = fake_dataset) <- c("never_smoked", "former_smoker", "current_smoker")

fake_dataset
#>       never_smoked former_smoker current_smoker
#>  [1,]            0             1              0
#>  [2,]            0             1              0
#>  [3,]            0             1              0
#>  [4,]            1             0              0
#>  [5,]            0             1              0
#>  [6,]            0             1              0
#>  [7,]            0             1              0
#>  [8,]            0             1              0
#>  [9,]            1             0              0
#> [10,]            1             0              0
#> [11,]            0             0              1
#> [12,]            1             0              0
#> [13,]            1             0              0
#> [14,]            0             0              1
#> [15,]            0             0              1
#> [16,]            0             1              0
#> [17,]            0             0              1
#> [18,]            0             0              1
#> [19,]            0             0              1
#> [20,]            0             1              0

updated_fake_dataset <- cbind(fake_dataset, "smoke_habbit" = (max.col(m = fake_dataset) - 1))

updated_fake_dataset
#>       never_smoked former_smoker current_smoker smoke_habbit
#>  [1,]            0             1              0            1
#>  [2,]            0             1              0            1
#>  [3,]            0             1              0            1
#>  [4,]            1             0              0            0
#>  [5,]            0             1              0            1
#>  [6,]            0             1              0            1
#>  [7,]            0             1              0            1
#>  [8,]            0             1              0            1
#>  [9,]            1             0              0            0
#> [10,]            1             0              0            0
#> [11,]            0             0              1            2
#> [12,]            1             0              0            0
#> [13,]            1             0              0            0
#> [14,]            0             0              1            2
#> [15,]            0             0              1            2
#> [16,]            0             1              0            1
#> [17,]            0             0              1            2
#> [18,]            0             0              1            2
#> [19,]            0             0              1            2
#> [20,]            0             1              0            1

Hope this helps.

Note

Please provide a REPRoducible EXample of your problem in your future threads. It provides more specifics of your problem, and it helps others to understand what problem you are facing.

If you don't know how to do it, take a look at this thread:

FAQ: How to do a minimal reproducible example ( reprex ) for beginners

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