作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一个字符串如下
"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+")
但是如何在同一个字符串中捕获所有内容以便添加它们呢?
最佳答案
zoo
包中的 rollapply
的另一个想法,
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))
关于r - 如何对R中字符串中数字的所有实例求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38715162/
我是一名优秀的程序员,十分优秀!