Home > Net >  Converting the values of a column if their value in another is less than X
Converting the values of a column if their value in another is less than X

Time:10-02

My data looks like this...

structure(list(id = c(591, 503, 839, 426, 436, 777, 224, 665, 
112, 201, 796, 569, 669, 344, 640, 749, 658, 181, 571, 350, 294, 
397, 270, 77, 803, 652, 466, 41, 564, 236, 11, 619, 110, 94, 
313, 348, 437, 202, 422, 284, 91, 734, 576, 22, 499, 351, 115, 
680, 608, 766, 97, 293, 148, 33, 509, 609, 59, 578, 197, 727, 
71, 521, 642, 326, 738, 417, 67, 192, 520, 719, 30, 57, 279, 
199, 388, 567, 596, 63, 99, 404, 611, 669, 98, 264, 61, 52, 230, 
316, 279, 529, 77, 631, 299, 95, 235, 717, 515, 331, 222, 384
), type = c("O", "L", "O", "L", "O", "O", "L", "L", "M", "M", 
"O", "O", "L", "L", "L", "L", "O", "M", "O", "L", "L", "L", "L", 
"M", "L", "O", "L", "M", "L", "L", "M", "O", "M", "M", "L", "L", 
"O", "L", "O", "L", "M", "L", "O", "M", "O", "L", "M", "L", "L", 
"L", "M", "O", "L", "M", "O", "O", "M", "L", "L", "O", "M", "L", 
"L", "L", "O", "L", "O", "L", "L", "O", "L", "M", "L", "O", "L", 
"O", "L", "L", "M", "L", "O", "L", "M", "L", "L", "M", "L", "O", 
"L", "O", "L", "L", "L", "M", "M", "O", "O", "L", "L", "O"), 
    salience = c(NA, NA, NA, NA, NA, NA, NA, NA, 4.91, 3.86, 
    NA, NA, NA, NA, NA, NA, NA, 4.95, NA, NA, NA, NA, NA, 2.4, 
    NA, NA, NA, 3.43, NA, NA, 3.65, NA, 3.29, 3.95, NA, NA, NA, 
    NA, NA, NA, 4.9, NA, NA, 2.68, NA, NA, 3.6, NA, NA, NA, 3.45, 
    NA, NA, 3.7, NA, NA, 4.05, NA, NA, NA, 3, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, 3.45, NA, NA, NA, NA, NA, NA, 4.86, 
    NA, NA, NA, 3.45, NA, NA, 3.86, NA, NA, NA, NA, NA, NA, NA, 
    4.29, 2.05, NA, NA, NA, NA, NA), active = c(5.85, 7.73, 5.75, 
    8.25, 5.7, 5.8, 2.1, 6.1, 1.8, 2.25, 5.25, 4.85, 6.75, 1.45, 
    6.45, 6.3, 5.85, 8.5, 5.55, 1.45, 6.63, 2.33, 2.2, 6.3, 6.28, 
    6, 6.75, 2.13, 6.72, 3.5, 2.33, 5.55, 3.05, 3.43, 2.75, 2.05, 
    5.88, 7, 4.7, 2.25, 2.65, 6.45, 5.1, 2.73, 5.05, 8.5, 2.15, 
    7.65, 7.05, 6.5, 2.15, 4.65, 7.8, 4.2, 5.05, 5.52, 3.15, 
    6.4, 7.3, 4.95, 3.03, 6.72, 6.3, 1.9, 5.3, 6.2, 4.03, 7.04, 
    6.15, 4.15, 2.33, 2.15, 6.95, 4.7, 7.13, 5.9, 6.4, 3.36, 
    2.1, 2.33, 4.6, 6.05, 2.15, 2.45, 7.52, 2.13, 6.05, 5.4, 
    7.33, 5.95, 6.4, 6.73, 6.75, 2.3, 7.15, 5.85, 6, 6.35, 2.08, 
    5.88)), row.names = c(NA, -100L), class = c("tbl_df", "tbl", 
"data.frame"))

I need to change the names of some of the values that are within the "type" column considering their value in the "salience" column.

What I need is to convert the M's in the "type" column into L's if their value in the "salience" column is less than 3.

So, what I want is that if I have an M in the "type" column that has a value smaller than 3 in the salience column, then change the M value for an L value.

IF>

type  salience
M       2.99

THEN>

type  salience
L      2.99

(the above result is a simplification)

CodePudding user response:

Using base R.

 df[df$type=="M" & df$salience < 3,]$type = "L"

CodePudding user response:

Using a simple ifelse function

df$type1= ifelse(df$type == 'M' & df$salience <3, 'L', df$type)

type1 is the new column.

CodePudding user response:

library(dplyr)
dat %>% 
    rowwise() %>% 
    mutate(type = if_else(salience < 3 & type == "M" , "L", "M")) 
      id type salience active
  1: 591    O       NA   5.85
  2: 503    L       NA   7.73
  3: 839    O       NA   5.75
  4: 426    L       NA   8.25
  5: 436    O       NA   5.70
  6: 777    O       NA   5.80
  7: 224    L       NA   2.10
  8: 665    L       NA   6.10
  9: 112    M     4.91   1.80
 10: 201    M     3.86   2.25
 11: 796    O       NA   5.25
 12: 569    O       NA   4.85

CodePudding user response:

Using case_when

library(dplyr)
df %>% 
   mutate(type = case_when(salience < 3 & type == "M" ~ "L", TRUE ~ "M"))
# A tibble: 100 × 4
      id type  salience active
   <dbl> <chr>    <dbl>  <dbl>
 1   591 M        NA      5.85
 2   503 M        NA      7.73
 3   839 M        NA      5.75
 4   426 M        NA      8.25
 5   436 M        NA      5.7 
 6   777 M        NA      5.8 
 7   224 M        NA      2.1 
 8   665 M        NA      6.1 
 9   112 M         4.91   1.8 
10   201 M         3.86   2.25
# … with 90 more rows
  •  Tags:  
  • r
  • Related