how to repeat a command several times by changing just one parameter

Hello,

In my console I created this command and I wanted to know if it was to repeat it several times. Indeed, I would like to repeat it more than ten times by changing the "#####" by "#####2", "#####3", ...

''''
Tibble4 <- Tibble |>
select(Joueurs,
Date,
Humeur) |>
na.omit() |>
filter(Joueurs == "#####1") |>
mutate(Zscore = (Humeur - mean(Humeur)) / sd(Humeur)) |>
mutate(Bornes = (case_when(Zscore >= '1.65' ~ "Bénéfique",
Zscore <= '- 1.65' ~ "Délétère",
TRUE ~ "Normal")))

ggplot(data = Tibble4) +
aes(x = Date,
y = Zscore,
color = Bornes) +
ylim(-3, 3) +
geom_point() + geom_line()
''''

Is there a command to do this in order to avoid copy and paste.

Thanks in advance

You can wrap your code in a function and call the function with different values. Here is a simple example.

library(ggplot2)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(purrr)
DF <- data.frame(Name = rep(c("A","B","C","D"), each = 4),
                 X = rep(1:4),
                 Y = rnorm(16))
Names <- c("A","B","C","D")
PlotFunc <- function(Nm) {
  Plt <- DF |> filter(Name == Nm) |> 
    ggplot(aes(X, Y)) + geom_point() + labs(title = Nm)
  print(Plt)
}
walk(Names, PlotFunc)

Created on 2023-01-16 with reprex v2.0.2

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.