1

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,

1
  • Try to wrap your samples in CODE TAGS {} button and try to select any one of the answer as correct answer to complete any thread, cheers and happy learning. Commented Jul 11, 2018 at 17:28

2 Answers 2

1

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"
Sign up to request clarification or add additional context in comments.

2 Comments

I think you need gsub(".*_|\\d+$", "", val3)
@akrun, ahh missed it, thanks for letting me know sir you ROCK, changed it now.
0

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"

data

str1 <- c("signi_BTM_Cearly2", "signi_BTM_Clate2", "signi_BTM_Rfollow2")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.