是否有R函数在特定单词后提取数值?

问题描述 投票:-1回答:1

寻找R中从数据表中提取以下内容的方法。

我想将数字取磅之前的数值。我希望利用“ ####磅”组合之前和之后的数据字段杂乱无章地提取数据。

如果输入是这个:x

我需要将磅输出分别解析:y

r extract text-extraction data-extraction
1个回答
0
投票

如果“ #####磅”在每个记录中仅出现一次,则使用正则表达式将起作用:

x <- c("14 trucks and 3298 pounds of tuna",
       "228 gallons and 190 pounds of sand",
       "161751 barrels gell, 3438540 pounds proppant",
       "29 pounds of hay, 100 barrels of water, 30 pins")

y <- gsub("(^|.* )(\\d+) pounds.*$", "\\2", x)
© www.soinside.com 2019 - 2024. All rights reserved.