CroatiaHR
CroatiaHR

Reputation: 625

R maping values to a new column

This is my dataset:

df = read.table(sep=",",  
    header=T,   
    text="combination,priority,boolean,value      
        0,1,True,1.4    
        0,2,True,2.0   
        1,1,True,3.2  
        2,2,True,54.2  
        3,1,False,12.1  
        4,2,False,44.1  
        ")

I dont know how to map the values to a new column. For example, I want to have a further column called group. Now I am looking for a neat way to do the following:

if combination -> group_value:  
0->0
1,2->1
3,4->2

So my expected output would be:

df = read.table(sep=",",  
        header=T,   
        text="combination,priority,boolean,value,group      
            0,1,True,1.4,0    
            0,2,True,2.0,0 
            1,1,True,3.2,1  
            2,2,True,54.2,1  
            3,1,False,12.1,2
            4,2,False,44.1,2
            ")

Upvotes: 0

Views: 40

Answers (2)

Dan Adams
Dan Adams

Reputation: 5204

library(dplyr)

df %>% 
     mutate(group = case_when(
             combination = 0 ~ 0, 
             combination %in% c(1,2) ~ 1, 
             combination %in% c(3,4) ~ 2
               ))

Upvotes: 1

Sinh Nguyen
Sinh Nguyen

Reputation: 4487

Another way using cut function if your base column is a numeric column

# Cut the combination into group with labels. The break is define base on
# the example provided in OP
df$group <- cut(df$combination, breaks = c(0, 1, 3, Inf), labels = c(0, 1, 2),
  include.lowest = TRUE, right = FALSE)

df
#>   combination priority boolean value group
#> 1           0        1    True   1.4     0
#> 2           0        2    True   2.0     0
#> 3           1        1    True   3.2     1
#> 4           2        2    True  54.2     1
#> 5           3        1   False  12.1     2
#> 6           4        2   False  44.1     2

# note that group here though it look like number it was a factor column
summary(df)
#>   combination       priority     boolean              value       group
#>  Min.   :0.000   Min.   :1.0   Length:6           Min.   : 1.40   0:2  
#>  1st Qu.:0.250   1st Qu.:1.0   Class :character   1st Qu.: 2.30   1:2  
#>  Median :1.500   Median :1.5   Mode  :character   Median : 7.65   2:2  
#>  Mean   :1.667   Mean   :1.5                      Mean   :19.50        
#>  3rd Qu.:2.750   3rd Qu.:2.0                      3rd Qu.:36.10        
#>  Max.   :4.000   Max.   :2.0                      Max.   :54.20
class(df$group)
#> [1] "factor"

Created on 2021-04-15 by the reprex package (v2.0.0)

Upvotes: 1

Related Questions