简体   繁体   English

从由符号分隔的R向量中的字符串中提取字符

[英]Extract character from string in R vector separated by symbol

Hi I have a vector of string in R which are separated by @ , I want to extract words separated by @..Example 嗨,我在R中有一个字符串向量,该向量由@分隔,我想提取由@。分隔的单词。

tweets =c( " @john @tom it is wonderful ", "@neel it is awesome ", "it is awesome")

I want a matrix/data.frame of names only with no text like this as output 我只希望没有名称的矩阵/data.frame像这样作为输出

X1=c("john","tom') 
X2 =c("neel",NA) , x3 = (NA,NA), data frame = as.data.frame(X1,X2,x3)

How can I do it? 我该怎么做?

A base R option would be to extract using gregexpr/regmatches and then pad NA s to the list elements with length<- and convert to a matrix base R选项是使用gregexpr/regmatches提取,然后将NA填充到length<-list元素中,然后转换为matrix

lst <- regmatches(tweets, gregexpr("(?<=@)\\w+", tweets, perl = TRUE))
do.call(rbind, lapply(lst, `length<-`, max(lengths(lst))))
#     [,1]   [,2] 
#[1,] "john" "tom"
#[2,] "neel" NA   
#[3,] NA     NA   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM