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
substr
extracts a substring by position:
substr('ABC_EFG_HIG_ADF_AKF_MNB', 9, 11)
returns
[1] "HIG"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With