Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract text after a symbol in R

sample1 = read.csv("pirate.csv")
sample1[,7] 
[1] >>xyz>>hello>>mate 1
[2] >>xyz>>hello>>mate 2
[3] >>xyz>>mate 3
[4] >>xyz>>mate 4
[5] >>xyz>>hello>>mate 5
[6] >>xyz>>hello>>mate 6

I have to extract and create an array which contains all the words after last >>.

How to do this?

Also, How can I extract (a) o qwerty, (b) mate1 and (c) pirate1 in different variables from the following string

p= '>>xyz- o qwerty>>hello>>mate1>>sole pirate1'

Thanks

like image 717
Looper Avatar asked May 05 '16 12:05

Looper


People also ask

How do I extract part of text in R?

The substring function in R can be used either to extract parts of character strings, or to change the values of parts of character strings. substring of a vector or column in R can be extracted using substr() function. To extract the substring of the column in R we use functions like substr() and substring().

How do I extract a string before a character in python?

You can extract a substring from a string before a specific character using the rpartition() method. What is this? rpartition() method partitions the given string based on the last occurrence of the delimiter and it generates tuples that contain three elements where.


2 Answers

x <- c('>>xyz>>hello>>mate 1', '>>xyz>>hello>>mate 2', '>>xyz>>mate 3', ' >>xyz>>mate 4' ,'>>xyz>>hello>>mate 5')
sub('.*>>', '', x)
#[1] "mate 1" "mate 2" "mate 3" "mate 4" "mate 5"
like image 115
Sotos Avatar answered Oct 01 '22 01:10

Sotos


Assuming you already read that stuff into an R data frame, you can use stringr package as follows:

library(stringr)
str_extract(df$mystring, '\\S+$')

For example, if you have string like this:

s <- '>>hello1>>hola1>>ahoy mate1'

You get:

str_extract(s, '\\S+$')
[1] "mate1"
like image 28
Gopala Avatar answered Oct 01 '22 00:10

Gopala