Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract the last word between | |

Tags:

regex

r

stringr

I have the following dataset

> head(names$SAMPLE_ID)
[1] "Bacteria|Proteobacteria|Gammaproteobacteria|Pseudomonadales|Moraxellaceae|Acinetobacter|"
[2] "Bacteria|Firmicutes|Bacilli|Bacillales|Bacillaceae|Bacillus|"                            
[3] "Bacteria|Proteobacteria|Gammaproteobacteria|Pasteurellales|Pasteurellaceae|Haemophilus|" 
[4] "Bacteria|Firmicutes|Bacilli|Lactobacillales|Streptococcaceae|Streptococcus|"             
[5] "Bacteria|Firmicutes|Bacilli|Lactobacillales|Streptococcaceae|Streptococcus|"             
[6] "Bacteria|Firmicutes|Bacilli|Lactobacillales|Streptococcaceae|Streptococcus|" 

I want to extract the last word between || as a new variable i.e.

Acinetobacter
Bacillus
Haemophilus

I have tried using

library(stringr)
names$sample2 <-   str_match(names$SAMPLE_ID, "|.*?|")
like image 557
Keniajin Avatar asked Dec 17 '15 19:12

Keniajin


People also ask

How do I extract text between two files?

To extract part string between two different characters, you can do as this: Select a cell which you will place the result, type this formula =MID(LEFT(A1,FIND(">",A1)-1),FIND("<",A1)+1,LEN(A1)), and press Enter key. Note: A1 is the text cell, > and < are the two characters you want to extract string between.

How do I extract text before last space?

Extract text before or after space with formula in Excel You can quickly extract the text before space from the list only by using formula. Select a blank cell, and type this formula =LEFT(A1,(FIND(" ",A1,1)-1)) (A1 is the first cell of the list you want to extract text) , and press Enter button.


1 Answers

We can use

library(stringi)
stri_extract_last_regex(v1, '\\w+')
#[1] "Acinetobacter"

data

v1 <- "Bacteria|Proteobacteria|Gammaproteobacteria|Pseudomonadales|Moraxellaceae|Acinetobacter|"
like image 106
akrun Avatar answered Sep 19 '22 15:09

akrun