我的文字看起来像这样:
a <- "233,236,241 solitude ΔE=1.9"
我想要做的是在两个空格()之间提取第二个单词,给出这个输出
> solitude
我尝试了两种方法:
a1 <- strsplit(a,' ',fixed=TRUE)[[1]][2]
a2 <- sapply(strsplit(a, " ", fixed=TRUE), "[", 2)
但它始终显示:
ΔE=1.9
这样做的正确方法是什么?
答案 0 :(得分:1)
试试这个:
gsub("\\s.+$","",gsub("^.+[[:digit:]]\\s","",a))
答案 1 :(得分:0)
这是一种使用捕获类(括号内的模式)和字符类(square brackest中的模式)的方法。
sub("(^[^ ]*[ ])([^ ]*)([ ].*$)" , "\\2", a)
[1] "solitude"
注释第一个捕获类模式:
"(^[^ ]*[ ])([^ ]*)([ ].*$)" , "\\2", a)
\finds first space
\ an arbitrary number of times
\\ inside a character class an '^' as the first character ...
signals negation of character class. This one with only the space character in it.
\----- '^' marks the beginning of a character value
第二个捕获类模式:
"(^[^ ]*[ ])([^ ]*)([ ].*$)" , "\\2", a)
\ an arbitrary number of times
\\negation of character class with only the space character in it.
第三次捕获类:
"(^[^ ]*[ ])([^ ]*)([ ].*$)" , "\\2", a)
\ the second space
\\anything after second space to end.
"\\<n>"
中的replacement
条目引用捕获类与它们在pattern
参数中出现的顺序相匹配。