Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting part of string by position in R

Tags:

regex

r

I have a vector of strings string which look like this

ABC_EFG_HIG_ADF_AKF_MNB

Now from each of this element I want to extract the 3rd set of strings(from left) i.e in this case HIG. How can I achieve this in R

like image 853
Rajarshi Bhadra Avatar asked Nov 28 '22 08:11

Rajarshi Bhadra


2 Answers

substr extracts a substring by position:

substr('ABC_EFG_HIG_ADF_AKF_MNB', 9, 11)

returns

[1] "HIG"
like image 57
alistaire Avatar answered Dec 06 '22 16:12

alistaire


Here's one more possibility:

strsplit(str1,"_")[[1]][3]
#[1] "HIG"

The command strsplit() does what its name suggests: it splits a string. The second parameter is the character on which the string is split, wherever it is found within the string.

Perhaps somewhat surprisingly, strsplit() returns a list. So we can either use unlist() to access the resulting split parts of the original string, or in this case address them with the index of the list [[1]] since the list in this example has only one member, which consists of six character strings (cf. the output of str(strsplit(str1,"_"))). To access the third entry of this list, we can specify [3] at the end of the command.

The string str1 is defined here as in the answer by @akrun.

like image 27
RHertel Avatar answered Dec 06 '22 17:12

RHertel