In My_CODE below, I was wondering if there might be a way to replace endsWith() with another function that captures the pattern anywhere (not just the end of a string)?
I tried tidyselect::contains but it seems it doesn't work in this case.
DATA <- read.table(header=TRUE,text =
"
Row Variables
1 advanced_baseline
2 beginner_baseline
3 intermediate_baseline
4 advanced_post1
5 beginner_post1
6 intermediate_post1
7 advanced_post2
8 beginner_post2
9 intermediate_post2")
library(tidyverse)
My_CODE <-
DATA |>
filter(!endsWith(Variables, "baseline")) |> #!!! CAN WE REPLACE `endWith` with `contain`
mutate(Variables2 = sub("post\\d+", "baseline", Variables),
Variables = paste0("(", Variables, " - ", Variables2, ")")) |>
right_join(filter(DATA, endsWith(Variables, "baseline")), #!!! CAN WE REPLACE `endWith` with `contain`
by = c("Variables2" = "Variables"), suffix = c("_post", "_pre")) |>
summarize(Variables, Row = list(c(Row_post, -Row_pre)), .by = Variables) |>
tibble::deframe()