In My_list
below, I want to modify the elements' names based on the following logic:
Pick the repeated word in the name (ex. beginner
in "(beginner_post1 - beginner_baseline)"
)
Pick the numeric in the NON-repeated name (ex. 1
in "post1"
)
Modify the name to "Gain_numeric(repeated word)"
(ex. Gain_1(beginner)
)
Is it possible to apply this logic to achieve my Desired_output
below?
My_list =
list("(beginner_post1 - beginner_baseline)"= c(5,-2),
"(intermediate_post1 - intermediate_baseline)"= c(6,-3),
"(advanced_post1 - advanced_baseline)"= c(4,-1),
"(beginner_post2 - beginner_baseline)"= c(8,-2),
"(intermediate_post2 - intermediate_baseline)"= c(9,-3),
"(advanced_post2 - advanced_baseline)"= c(7,-1),
"(framework notes_posttest1 - framework notes_baseline)"=c(1,2),
"(note-taking instruction_posttest1 - note-taking instruction_baseline)"=1
)
Desired_output =
list("Gain_1(beginner)"= c(5,-2),
"Gain_1(intermediate)"= c(6,-3),
"Gain_1(advanced)"= c(4,-1),
"Gain_2(beginner)"= c(8,-2),
"Gain_2(intermediate)"= c(9,-3),
"Gain_2(advanced)"= c(7,-1),
"Gain_1(framework notes)" = c(1,2),
"Gain_1(note-taking instruction)"=1
)
res <- setNames(My_list, sub("^.([^_]+)\\D+(\\d+).*", "Gain_\\2(\\1)", names(My_list)))
$`Gain_1(beginner)`
[1] 5 -2
$`Gain_1(intermediate)`
[1] 6 -3
$`Gain_1(advanced)`
[1] 4 -1
$`Gain_2(beginner)`
[1] 8 -2
$`Gain_2(intermediate)`
[1] 9 -3
$`Gain_2(advanced)`
[1] 7 -1
$`Gain_1(framework notes)`
[1] 1 2
$`Gain_1(note-taking instruction)`
[1] 1