在R中的符号后提取文本

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

我必须提取并创建一个数组,其中包含最后一个 >> 之后的所有单词。

如何操作?

此外,如何从以下字符串中提取不同变量中的 (a) o qwerty、(b) mate1 和 (c) pirate1

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

谢谢

假设您已经将这些内容读入 R 数据框,您可以按如下方式使用 stringr 包:

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

例如,如果您有这样的字符串:

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

你得到:

str_extract(s, '\S+$')
[1] "mate1"
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"