Bruh
Bruh

Reputation: 285

mutate across columns with ifelse

I'm trying to mutate values across multiple columns to values from another column.

This is my dataset:

library(stringr)
library(dplyr)
library(fastDummies)

score <- sample(1:100,20,replace=TRUE)
df <- data.frame(score)

df <- df %>%
   mutate(grp = cut(score, breaks = c(-Inf, seq(0, 100, by = 20), Inf)), 
      grp = str_c("G", as.integer(droplevels(grp)), '_', 
      str_replace(grp, '\\((\\d+),(\\d+)\\]', 
     '\\1_\\2'))) %>% 
   dummy_cols("grp", remove_selected_columns = TRUE) %>% 
   rename_with(~ str_remove(.x, 'grp_'), starts_with('grp_'))

I want to mutate columns that start with the letter "G", so G1_0_20, G2_20_40, etc.

If columns that start with G (G1_0_20, G2_20_40,etc) has value of 1, then its value should match column "Score", otherwise NA.

I can't quite figure out how to use mutate across with ifelse statement.

I would appreciate all the help there is! Thanks!!!

Upvotes: 0

Views: 2015

Answers (2)

Chris Ruehlemann
Chris Ruehlemann

Reputation: 21400

I think this is it:

df %>%
  mutate(across(starts_with("G"), ~ifelse(. == 1, score, NA)))
   score G1_0_20 G2_20_40 G3_40_60 G4_60_80 G5_80_100
1     52      NA       NA       52       NA        NA
2     90      NA       NA       NA       NA        90
3     73      NA       NA       NA       73        NA
4     11      11       NA       NA       NA        NA
5     16      16       NA       NA       NA        NA
6     47      NA       NA       47       NA        NA
7     42      NA       NA       42       NA        NA
8     62      NA       NA       NA       62        NA
9     64      NA       NA       NA       64        NA
10    25      NA       25       NA       NA        NA
11    47      NA       NA       47       NA        NA
12    63      NA       NA       NA       63        NA
13    96      NA       NA       NA       NA        96
14    95      NA       NA       NA       NA        95
15     3       3       NA       NA       NA        NA
16    25      NA       25       NA       NA        NA
17    78      NA       NA       NA       78        NA
18    10      10       NA       NA       NA        NA
19    51      NA       NA       51       NA        NA
20    12      12       NA       NA       NA        NA

Upvotes: 1

stefan
stefan

Reputation: 124083

Instead of creating dummies and replacing using an ifelse one option would be to use tidyr::pivot_wider:

library(stringr)
library(dplyr, warn=FALSE)
library(tidyr)

set.seed(123)

score <- sample(1:100, 20, replace = TRUE)
df <- data.frame(score)

df %>%
  mutate(rowid = row_number()) |>
  mutate(
    grp = cut(score, breaks = c(-Inf, seq(0, 100, by = 20), Inf)),
    grp = str_c(
      "G", as.integer(droplevels(grp)), "_",
      str_replace(
        grp, "\\((\\d+),(\\d+)\\]",
        "\\1_\\2"
      )
    ),
    score1 = score
  ) |>
  tidyr::pivot_wider(names_from = grp, values_from = score1) |>
  select(-rowid)
#> # A tibble: 20 × 6
#>    score G2_20_40 G4_60_80 G3_40_60 G1_0_20 G5_80_100
#>    <int>    <int>    <int>    <int>   <int>     <int>
#>  1    31       31       NA       NA      NA        NA
#>  2    79       NA       79       NA      NA        NA
#>  3    51       NA       NA       51      NA        NA
#>  4    14       NA       NA       NA      14        NA
#>  5    67       NA       67       NA      NA        NA
#>  6    42       NA       NA       42      NA        NA
#>  7    50       NA       NA       50      NA        NA
#>  8    43       NA       NA       43      NA        NA
#>  9    14       NA       NA       NA      14        NA
#> 10    25       25       NA       NA      NA        NA
#> 11    90       NA       NA       NA      NA        90
#> 12    91       NA       NA       NA      NA        91
#> 13    69       NA       69       NA      NA        NA
#> 14    91       NA       NA       NA      NA        91
#> 15    57       NA       NA       57      NA        NA
#> 16    92       NA       NA       NA      NA        92
#> 17     9       NA       NA       NA       9        NA
#> 18    93       NA       NA       NA      NA        93
#> 19    99       NA       NA       NA      NA        99
#> 20    72       NA       72       NA      NA        NA

Upvotes: 0

Related Questions