[英]Extract min and max value from a character variable with R
I have a df with a variable containing multiple charactere as unit and value like below我有一个 df 变量,其中包含多个字符作为单位和值,如下所示
[525] "8 µg/ml"
[526] "16 µg/ml - 32 µg/ml - 200 µg/ml - 500 µg/ml - 1000 µg/ml"
[527] "5 µg/ml - 10 µg/ml - 250 µg/ml"
[528] "20 µg/ml"
[529] "16 µg/ml"
[530] "60 µg/ml"
I would like to extract two values (min and max) from this variable in two different other variables When only one value is available i would like to implemente min by default I have tried to used str_extracted but i'm sur you will have more valuable advice or solutions Thanks to all of you for your help Best我想在两个不同的其他变量中从这个变量中提取两个值(最小值和最大值)当只有一个值可用时,我想默认实现 min 我尝试使用 str_extracted 但我相信你会有更多的价值建议或解决方案感谢大家的帮助最好的
You can extract all the numbers from the string using str_extract_all
and then return min and max value using range
.您可以使用
str_extract_all
从字符串中提取所有数字,然后使用range
返回最小值和最大值。
mat <- t(sapply(stringr::str_extract_all(x, '\\d+'), function(x)
range(as.numeric(x))))
mat[mat[, 1] == mat[, 2], 2] <- NA
mat
# [,1] [,2]
#[1,] 8 NA
#[2,] 16 1000
#[3,] 5 250
#[4,] 20 NA
#[5,] 16 NA
#[6,] 60 NA
data数据
x <- c("8 µg/ml", "16 µg/ml - 32 µg/ml - 200 µg/ml - 500 µg/ml - 1000 µg/ml",
"5 µg/ml - 10 µg/ml - 250 µg/ml", "20 µg/ml", "16 µg/ml", "60 µg/ml")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.