0

I’m trying to create multiple new score columns based on other columns. I’d like to use a function to minimize copy pasting large blocks of code.

I’m trying to do something like:

Myfunction <- function(column){

 Column_df <- old_df %>%

  mutate(column.score = if_else(column = 1, “yes”, “no”)

   )

 }

Score_df <- Myfunction(c(math, reading, science)))

But I’m getting an error saying object math is not found

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • This cannot work for several reasons. Please provide sample data, see `?dput()`. – Pax Jan 18 '22 at 20:04
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jan 18 '22 at 20:28

1 Answers1

0

Starting with an example data frame as below

df <- purrr::map_dfc(c('math', 'reading', 'science', 'history'),
                     ~ rlang::list2(!!.x := sample(1:3, 10, TRUE)))
df
#> # A tibble: 10 × 4
#>     math reading science history
#>    <int>   <int>   <int>   <int>
#>  1     2       1       3       1
#>  2     3       2       3       1
#>  3     2       2       2       2
#>  4     2       3       1       2
#>  5     3       3       1       2
#>  6     1       2       3       2
#>  7     3       3       2       1
#>  8     3       3       3       2
#>  9     1       2       2       1
#> 10     2       2       2       3

You can create new "score" columns with a function by passing your columns argument to across inside {{ }}, and using the .name option to add ".score" to the name.

If you want only the "score" columns in the output, rather than to add them to existing columns, use transmute instead of mutate.

library(dplyr, warn.conflicts = FALSE)

Myfunction <- function(df, columns){
  df %>% 
    mutate(across({{ columns }}, ~ if_else(. == 1, 'yes', 'no'),
                  .names = '{.col}.score'))
}

df %>% 
  Myfunction(c(math, reading, science))
#> # A tibble: 10 × 7
#>     math reading science history math.score reading.score science.score
#>    <int>   <int>   <int>   <int> <chr>      <chr>         <chr>        
#>  1     2       1       3       1 no         yes           no           
#>  2     3       2       3       1 no         no            no           
#>  3     2       2       2       2 no         no            no           
#>  4     2       3       1       2 no         no            yes          
#>  5     3       3       1       2 no         no            yes          
#>  6     1       2       3       2 yes        no            no           
#>  7     3       3       2       1 no         no            no           
#>  8     3       3       3       2 no         no            no           
#>  9     1       2       2       1 yes        no            no           
#> 10     2       2       2       3 no         no            no

Created on 2022-01-18 by the reprex package (v2.0.1)

IceCreamToucan
  • 28,083
  • 2
  • 22
  • 38
  • im getting "No applicable useMethod for function"? – bootcampwill Jan 18 '22 at 20:55
  • If the error you got was actually "Error in UseMethod("mutate") : no applicable method for 'mutate' applied to an object of class "function"", then one possible explanation is that you're running the code without first either (a) defining a data frame called `df` or (b) changing `df` in the code to the name of your existing data frame. I don't think the error you quoted is a real R error message. – IceCreamToucan Jan 18 '22 at 21:03