我正尝试使用mutate使用grepl(部分匹配)和ifelse创建新列。 我有单位名称的药物名称列表,我想用这些单位创建新列。 这是我的药品名称样本,
b = '2018-09-20 12:30'
a = as.Date(b)
这是我尝试过的代码,
Description
ZOLEDRONIC ACID INF 4MG/5ML (5ML)
CALCICHEW D3 FORTE CHEWABLE TABS
DAYCASE DRUGS
AKYNZEO (NETUPITANT/PALONOSETRON) HARD CAPS 300MG/0.5MG
CYCLOPHOSPHAMIDE INF 1MG/ML
EPIRUBICIN INJ 1MG/ML
DAYCASE DRUGS
DAYCASE DRUGS
ALOXI (PALONOSETRON) INJ 250MCG
PACLITAXEL INF 1MG/ML
DEXAMETHASONE VIAL 6.6MG/2ML - USE PSD742
PACLITAXEL INF 1MG/ML
RAMUCIRUMAB INF 0.1MG - 100MG
当前输出是这样的
units <- c("TABS","INF","SYR","CAPS","INJ","VIAL","SOL","POWDER","GEL","CREAM","LOTION","AMP"," SINGLEJECT","PFS")
drug_name <- drug_name %>% mutate(unit = grepl(paste(units,collapse = "|"),Description))
我不希望单位为TRUE / FALSE,而是应该指定确切的单位,例如这样
Description unit
ZOLEDRONIC ACID INF 4MG/5ML (5ML) TRUE
CALCICHEW D3 FORTE CHEWABLE TABS TRUE
DAYCASE DRUGS FALSE
AKYNZEO (NETUPITANT/PALONOSETRON) CAPS TRUE
CYCLOPHOSPHAMIDE INF 1MG/ML TRUE
EPIRUBICIN INJ 1MG/ML TRUE
DAYCASE DRUGS FALSE
DAYCASE DRUGS FALSE
我正在尝试重新编码此密码,但不知道如何执行此操作。 另外,我需要将药物名称(4MG / 5ML)中的指标提取到单独的列中。我可以在另一个查询中执行此操作,但需要使用单位执行单个查询。并非所有药物都具有这种格式,例如01.MG-100MG,250MCG。
ZOLEDRONIC ACID INF 4MG/5ML (5ML) INF
CALCICHEW D3 FORTE CHEWABLE TABS TABS
有人可以帮我解决这个问题吗?
答案 0 :(得分:3)
这是使用dplyr
和purrr
的一种方法。
library(dplyr)
library(purrr)
drug_name <- data_frame(
Description = trimws(readLines(textConnection('ZOLEDRONIC ACID INF 4MG/5ML (5ML)
CALCICHEW D3 FORTE CHEWABLE TABS
DAYCASE DRUGS
AKYNZEO (NETUPITANT/PALONOSETRON) HARD CAPS 300MG/0.5MG
CYCLOPHOSPHAMIDE INF 1MG/ML
EPIRUBICIN INJ 1MG/ML
DAYCASE DRUGS
DAYCASE DRUGS
ALOXI (PALONOSETRON) INJ 250MCG
PACLITAXEL INF 1MG/ML
DEXAMETHASONE VIAL 6.6MG/2ML - USE PSD742
PACLITAXEL INF 1MG/ML
RAMUCIRUMAB INF 0.1MG - 100MG')))
)
units <- c("TABS","INF","SYR","CAPS","INJ","VIAL","SOL","POWDER",
"GEL","CREAM","LOTION","AMP","SINGLEJECT","PFS")
units1 <- sprintf("\\b(%s)\\b", paste(units, collapse = "|"))
首先是一个概念演练:
m <- regmatches(drug_name$Description,
gregexpr(paste0(units1, ".*"), drug_name$Description))
m
# [[1]]
# [1] "INF 4MG/5ML (5ML)"
# [[2]]
# [1] "TABS"
# [[3]]
# character(0)
# [[4]]
# [1] "CAPS 300MG/0.5MG"
# [[5]]
# [1] "INF 1MG/ML"
# [[6]]
# [1] "INJ 1MG/ML"
# [[7]]
# character(0)
# [[8]]
# character(0)
# [[9]]
# [1] "INJ 250MCG"
# [[10]]
# [1] "INF 1MG/ML"
# [[11]]
# [1] "VIAL 6.6MG/2ML - USE PSD742"
# [[12]]
# [1] "INF 1MG/ML"
# [[13]]
# [1] "INF 0.1MG - 100MG"
我用.*
“吸引所有关注”,以便找到单位数量。有时这会得到比我们想要的更多的东西,但是为了在一个单元后找到所有类似值的单词,我们需要先将所有单词抓起来然后过滤。
我们需要防范character(0)
并理想地转换为向量,所以
fixEmpties <- function(lst, default=NA_character_)
unlist(replace(lst, lengths(lst) == 0L, default))
尽管您可能更喜欢使用空字符串""
而不是NA_character_
。
要过滤,这是一个快速功能,可以在以下每个“单词”中查找任何数字。它还允许单独的"-"
,但是如果最后一个匹配则将其删除。
extractAmounts <- function(s, default=NA_character_) {
vec <- strsplit(s, "\\s+")[[1]][-1]
vec2 <- which(cumall(grepl("[0-9]", vec) | vec == "-"))
if (isTRUE(length(vec2) > 0)) {
# remove a trailing "-" that does not result in a range
if (vec[ tail(vec2, 1) ] == "-") vec2 <- vec2[-length(vec2)]
return(paste(vec[vec2], collapse = " "))
} else return(default)
}
然后快速获取原始单位,总是第一个单词。
extractUnits <- function(s)
head(strsplit(s, "\\s+")[[1]], 1)
测试:
m <- fixEmpties(m)
sapply(m, extractUnits, USE.NAMES=FALSE)
# [1] "INF" "TABS" NA "CAPS" "INF" "INJ" NA NA "INJ" "INF"
# [11] "VIAL" "INF" "INF"
sapply(m, extractAmounts, USE.NAMES=FALSE)
# [1] "4MG/5ML (5ML)" NA NA "300MG/0.5MG"
# [5] "1MG/ML" "1MG/ML" NA NA
# [9] "250MCG" "1MG/ML" "6.6MG/2ML" "1MG/ML"
# [13] "0.1MG - 100MG"
好吧,现在将其放入管道:
drug_name %>%
mutate(
full = fixEmpties(regmatches(Description, gregexpr(paste0(units1, ".*"), Description))),
unit = map_chr(full, extractUnits),
amt = map_chr(full, extractAmounts)
) %>%
select(-full)
# # A tibble: 13 x 3
# Description unit amt
# <chr> <chr> <chr>
# 1 ZOLEDRONIC ACID INF 4MG/5ML (5ML) INF 4MG/5ML (5ML)
# 2 CALCICHEW D3 FORTE CHEWABLE TABS TABS <NA>
# 3 DAYCASE DRUGS <NA> <NA>
# 4 AKYNZEO (NETUPITANT/PALONOSETRON) HARD CAPS 300MG/0.5MG CAPS 300MG/0.5MG
# 5 CYCLOPHOSPHAMIDE INF 1MG/ML INF 1MG/ML
# 6 EPIRUBICIN INJ 1MG/ML INJ 1MG/ML
# 7 DAYCASE DRUGS <NA> <NA>
# 8 DAYCASE DRUGS <NA> <NA>
# 9 ALOXI (PALONOSETRON) INJ 250MCG INJ 250MCG
# 10 PACLITAXEL INF 1MG/ML INF 1MG/ML
# 11 DEXAMETHASONE VIAL 6.6MG/2ML - USE PSD742 VIAL 6.6MG/2ML
# 12 PACLITAXEL INF 1MG/ML INF 1MG/ML
# 13 RAMUCIRUMAB INF 0.1MG - 100MG INF 0.1MG - 100MG
答案 1 :(得分:1)
pattern = paste( units, collapse = "|" )
df %>% mutate(medicine = stringr::str_split( Description, pattern, simplify = TRUE )[,1],
medicine = stringr::str_trim( medicine ),
unit = stringr::str_extract( Description, pattern ),
volume = stringr::str_split( Description, pattern, simplify = TRUE )[,2],
volume = ifelse( volume == "", NA, volume ),
volume = stringr::str_trim( volume ) ) %>%
select( -Description )
# medicine unit volume
# 1 ZOLEDRONIC ACID INF 4MG/5ML (5ML)
# 2 CALCICHEW D3 FORTE CHEWABLE TABS <NA>
# 3 DAYCASE DRUGS <NA> <NA>
# 4 AKYNZEO (NETUPITANT/PALONOSETRON) HARD CAPS 300MG/0.5MG
# 5 CYCLOPHOSPHAMIDE INF 1MG/ML
# 6 EPIRUBICIN INJ 1MG/ML
# 7 DAYCASE DRUGS <NA> <NA>
# 8 DAYCASE DRUGS <NA> <NA>
# 9 ALOXI (PALONOSETRON) INJ 250MCG
# 10 PACLITAXEL INF 1MG/ML
# 11 DEXAMETHASONE VIAL 6.6MG/2ML - USE PSD742
# 12 PACLITAXEL INF 1MG/ML
# 13 RAMUCIRUMAB INF 0.1MG - 100MG