假设我有一个字符串如下
"The specimen is 34 x 1 x 5 mm and there is also another thing in there that is 22 x 4 x 1 mm and a further thing of 11 x 4 x 8 so this should be added together"
我想将每个东西的立方毫米加在一起,然后放在数据框中的单独字段中。
我有正则表达式来执行此操作,但我不知道如何捕获每个实例,以便将它们添加在一起。结果应该是1226 mm3
为了捕获一个实例,我使用:
chunky <- str_extract(EndoSubsetEMRSizes$NatureOfSpec, "\\d+\\s*x\\s*\\d+\\s*x\\s*\\d+")
但如何捕获同一个字符串中的所有内容以便可以添加它们?
另一个想法是rollapply
from zoo
包裹,
library(zoo)
library(stringr)
sum(rollapply(as.numeric(str_extract_all(x, '[0-9]+')[[1]]), 3, by = 3, prod))
#[1] 610
或将其应用于列,
sapply(EndoSubsetEMRSizes$NatureOfSpec, function(x)
sum(rollapply(as.numeric(str_extract_all(x, '[0-9]+')[[1]]), 3, by = 3, prod))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)