从r中的字符串变量中提取特定值
我有一个字符变量。我需要从从r中的字符串变量中提取特定值,r,string,extract,R,String,Extract,我有一个字符变量。我需要从title=”“值中提取信息。基本上,我需要标题=后面的中的所有值 以下是示例数据集: df <- data.frame( id = c(1,2,3), character = c('mrow><mn>2<mn><mi>h<mi><m title="h+r=2"><mst
title=”“
值中提取信息。基本上,我需要标题=
后面的中的所有值
以下是示例数据集:
df <- data.frame(
id = c(1,2,3),
character = c('mrow><mn>2<mn><mi>h<mi><m title="h+r=2"><mstyle',
'mrow><mn>2<mn><mi>h<mi><m title="r+2h=h"><mstyle&',
'mrow><mn>2<mn><mi>h<mi><m title="h∙rleft(frac{2h}{2}right)"><mstyle>'))
> df
id character
1 1 mrow><mn>2<mn><mi>h<mi><m title="h+r=2"><mstyle
2 2 mrow><mn>2<mn><mi>h<mi><m title="r+2h=h"><mstyle&
3 3 mrow><mn>2<mn><mi>h<mi><m title="h·rleft(frac{2h}{2}right)"><mstyle>
试试这个
library(dplyr)
df %>% mutate(character = sub(".+title=\"(.+)\".+", "\\1", character))
应使用regex101创建拟合正则表达式:
然后您可以使用str\u extract
获取值
或者使用tidyr的提取功能:
df %>% tidyr::extract(character, "title", regex="title=\"(.+)\"")
内(df,字符
df %>% tidyr::extract(character, "title", regex="title=\"(.+)\"")