我需要从文本注释中提取血压值,该文本注释通常报告为一个较大的数字,以较小的数字表示为“ /”,单位为mm HG(不是小数,只能这样写)。在下面的4个示例中,我只想提取114 / 46、135 / 67、109 / 50和188/98,在前后没有空格,并将最高编号放在名为SBP的列中,并将最低编号放在称为SBP的列中DBP。预先感谢您的协助。
bb <- c("PATIENT/TEST INFORMATION (m2): 1.61 m2\n BP (mm Hg): 114/46 HR 60 (bpm)", "PATIENT/TEST INFORMATION:\ 63\n Weight (lb): 100\nBSA (m2): 1.44 m2\nBP (mm Hg): 135/67 HR 75 (bpm)", "PATIENT/TEST INFORMATION:\nIndication: Coronary artery disease. Hypertension. Myocardial infarction.\nWeight (lb): 146\nBP (mm Hg): 109/50 HR (bpm)", "PATIENT/TEST INFORMATION:\nIndication: Aortic stenosis. Congestive heart failure. Shortness of breath.\nHeight: (in) 64\nWeight (lb): 165\nBSA (m2): 1.80 m2\nBP (mm Hg): 188/98 HR 140 (bpm) ")
BP <- head(bb,4)
dput(bb)
我们可以使用regmatches/regexpr
frombase R
来提取所需的值,然后使用来read.table
创建两列data.frame
read.table(text = regmatches(bb, regexpr('\\d+/\\d+', bb)),
sep="/", header = FALSE, stringsAsFactors = FALSE)
# V1 V2
#1 114 46
#2 135 67
#3 109 50
#4 188 98
或使用strcapture
从base R
strcapture( "(\\d+)\\/(\\d+)", bb, data.frame(X1 = integer(), X2 = integer()))
# X1 X2
#1 114 46
#2 135 67
#3 109 50
#4 188 98
要将其创建为原始data.frame中的新列,请使用任一cbind
方法将输出与原始数据集绑定
cbind(data, read.table(text = ...))
要么
data[c("V1", "V2")] <- read.table(text = ...)
或使用extract
从tidyr
library(dplyr)
library(tidyr)
tibble(bb) %>%
extract(bb, into = c("X1", "X2"), ".*\\b(\\d+)/(\\d+).*", convert = TRUE)
# A tibble: 4 x 2
# X1 X2
# <int> <int>
#1 114 46
#2 135 67
#3 109 50
#4 188 98
如果我们不想删除原始列,请remove = FALSE
在extract
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句