如何从字符串列中提取数字并确定它们是否小于阈值?

How to extract numbers from a string column and determine whether they are less than a threshold?

所以我有以下 df:

df=data.frame(strength=c("10MG;50MG","2MG;5MG","1MG,5MG","100MG;1UG"))

我想标记 <5 MG 的行, 这就是我所做的,但它不起作用,我收到错误

library(dplyr)    
df %>% mutate(new=as.numeric(unlist(str_extract_all(strength, "[0-9]+"))),check=any(which(new<5)))

str_extract_all returns 一个列表,所以如果你 unlist 他们你将失去关于哪个值来自哪一行的信息。将它们保存在一个列表中,然后使用 rowwisemap 函数之一遍历每个列表以检查该行中的 any 值是否小于 5。

library(dplyr)

df %>% 
  mutate(new = stringr::str_extract_all(strength, "[0-9]+")) %>%
  rowwise() %>%
  mutate(check = any(as.numeric(new) < 5))

# strength  new       check
#  <chr>     <list>    <lgl>
#1 10MG;50MG <chr [2]> FALSE
#2 2MG;5MG   <chr [2]> TRUE 
#3 1MG,5MG   <chr [2]> TRUE 
#4 100MG;1UG <chr [2]> TRUE