-1

I want to replace a particular string ("Yes") in only some columns in my dataframe. And I want to replace my string with the column headers.

I found similar solutions here, but they only work when replacing in the whole dataframe.

Any help will be really appreciated! Thanks

  • 1
    You'll get better answers if [make this question reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by including a small representative dataset in a plain text format - for example the output from `dput(yourdata)`, if that is not too large, any code that you tried and an example of the desired output. – neilfws May 09 '23 at 23:07

1 Answers1

1

In future, please include a minimal, reproducible example in your question to make it easier to answer (help us to help you). In this case, I believe you may have had difficulty with this step, so here is a reproducible example and potential solution using tidyverse functions and the open source palmer penguins dataset:

library(tidyverse)
library(palmerpenguins)

# insert "yes" into three of the columns
df <- penguins %>%
  na.omit() %>%
  mutate(across(c(bill_length_mm, flipper_length_mm, bill_depth_mm),
         ~ifelse(.x > 190 | .x < 38, "yes", .x)))

head(df)
#> # A tibble: 6 × 8
#>   species island    bill_length_mm bill_depth_mm flipper_l…¹ body_…² sex    year
#>   <fct>   <fct>     <chr>          <chr>         <chr>         <int> <fct> <int>
#> 1 Adelie  Torgersen 39.1           yes           181            3750 male   2007
#> 2 Adelie  Torgersen 39.5           yes           186            3800 fema…  2007
#> 3 Adelie  Torgersen 40.3           yes           yes            3250 fema…  2007
#> 4 Adelie  Torgersen yes            yes           yes            3450 fema…  2007
#> 5 Adelie  Torgersen 39.3           yes           190            3650 male   2007
#> 6 Adelie  Torgersen 38.9           yes           181            3625 fema…  2007
#> # … with abbreviated variable names ¹​flipper_length_mm, ²​body_mass_g

# replace "yes" in only two of the columns with the column name
df %>%
  mutate(across(c(bill_length_mm, flipper_length_mm),
                ~ifelse(.x == "yes", cur_column(), .x)))
#> # A tibble: 333 × 8
#>    species island    bill_length_mm bill_depth_mm flipper_…¹ body_…² sex    year
#>    <fct>   <fct>     <chr>          <chr>         <chr>        <int> <fct> <int>
#>  1 Adelie  Torgersen 39.1           yes           181           3750 male   2007
#>  2 Adelie  Torgersen 39.5           yes           186           3800 fema…  2007
#>  3 Adelie  Torgersen 40.3           yes           flipper_l…    3250 fema…  2007
#>  4 Adelie  Torgersen bill_length_mm yes           flipper_l…    3450 fema…  2007
#>  5 Adelie  Torgersen 39.3           yes           190           3650 male   2007
#>  6 Adelie  Torgersen 38.9           yes           181           3625 fema…  2007
#>  7 Adelie  Torgersen 39.2           yes           flipper_l…    4675 male   2007
#>  8 Adelie  Torgersen 41.1           yes           182           3200 fema…  2007
#>  9 Adelie  Torgersen 38.6           yes           flipper_l…    3800 male   2007
#> 10 Adelie  Torgersen bill_length_mm yes           flipper_l…    4400 male   2007
#> # … with 323 more rows, and abbreviated variable names ¹​flipper_length_mm,
#> #   ²​body_mass_g

Created on 2023-05-10 with reprex v2.0.2

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46
  • Thank you very much for your answer! It solved a big headache! Also I wanto to apologize for not including a shorter example. For sure, your example will help me to use different and shorter examples than my current datasets if I have another problem. I really appreciate your help! – Rafael Bravo May 10 '23 at 14:23