Reputation: 328
I have an object, res_list, that contains data that I would like to extract using a function.
res_list$ has the following possibilities: Cearly,Rearly,Clate,Rlate,Cfollow,Rfollow.
I want to create a function to extract data from all the different res_list$ possibilities, in an iterative manner, by utilising the 'name' of data objects I have already created in R
The names of these objects are as follows:
signi_BTM_Cearly2
signi_BTM_Clate2
.
.
.
signi_BTM_Rfollow2
Basically, I just want to tell the function I am creating to take only the "Cearly", "Clate",..."Rfollow" part of the table names. So I can retrieve the data that I need.
I know its a very simple task, I just can't figure it out.
Thank you all for your time,
Upvotes: 1
Views: 152
Reputation: 133700
Following simple use of gsub
may also help you here.
val3<-c("signi_BTM_Cearly2","signi_BTM_Clate2")
gsub(".*_|\\d+$","",val3)
Output will be as follows.
[1] "Cearly2" "Clate2"
Upvotes: 1
Reputation: 887691
We could use sub
to capture the characters after the _
as a group followed by one or more digits (\\d+
) at the end ($
) of the string and replace by the backreference (\\1
) of the captured group
sub(".*_([A-Za-z]+)\\d+$", "\\1", str1)
#[1] "Cearly" "Clate" "Rfollow"
str1 <- c("signi_BTM_Cearly2", "signi_BTM_Clate2", "signi_BTM_Rfollow2")
Upvotes: 0