我试图用grepl(部分匹配)和ifelse使用mutate创建新列。我有单位的药物名称列表,我想用这些单位创建新的列。这是我的药名样本,
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))
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
ZOLEDRONIC ACID INF 4MG/5ML (5ML) INF
CALCICHEW D3 FORTE CHEWABLE TABS TABS
ZOLEDRONIC ACID INF 4MG/5ML (5ML) INF 4MG/5ML
CALCICHEW D3 FORTE CHEWABLE TABS TABS NA
CYCLOPHOSPHAMIDE INF 1MG/ML INF 1MG/ML
ALOXI (PALONOSETRON) INJ 250MCG INF 250MCG
下面是一个使用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"
我用.*
“获取所有以下内容”,以便找到单位的数量。这有时会得到比我们想要的更多的东西,但是为了在一个单位后面找到所有类似值的单词,我们需要抓取所有的单词,然后进行过滤。
fixEmpties <- function(lst, default=NA_character_)
unlist(replace(lst, lengths(lst) == 0L, default))
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
问题内容: 和创建之间有什么区别?请告诉我 在String中,我们只有String对象,然后为什么要区别对待这两个对象。s1和s2具有不同的存储地址,而s3和s4具有相同的存储地址。为什么它基于 运算符。 问题答案: 加载定义Java字符串中代表字符串文字的对象的类时,它们将被添加到共享池中。这样可以确保String文字的所有“副本”实际上是同一对象……即使该文字出现在多个类中。这就是为什么是。
创建和有什么区别?请让我知道 在String中,我们只有String对象,那么为什么它将这两个对象区别对待。s1和s2具有不同存储器地址,而s3和s4具有相同的存储器地址。为什么它基于运算符工作?
我有一个名称重复的数据集。如果名称重复,我想创建一个值为1(TRUE)或0(FALSE)的新列。 这是我使用的代码: 或者 然而,我得到了上面可以看到的错误。 另一个想法是使用group_by,然后计算计数。喜欢: 但是,它不能返回原始数据帧后group_by
我正在研究一个数据帧(昏暗: 10,155乘33)。几排 对于变量age_band,我想使用tidyverse函数separate(),mutate() 将年龄段分成两列a1 我正在使用以下代码: 错误:列必须是长度10155(行数)或1,而不是0请建议可以做什么。当我在ifelse语句中运行没有'$'的代码时,我得到一个错误对象'a1'没有找到,而通常,我们在使用链接运算符时不需要'$' 也是
下面是我使用dplyr尝试的代码,
问题内容: 我知道问题的标题不是很清楚,对此感到抱歉,不知道如何提出。我有一个非常基本的Java实现问题,我想着重于应用程序性能,但是它也涉及Java中的String创建模式。 我了解Java中字符串的不变性概念。我不确定的是,我在某处读到以下内容不会创建两个不同的String对象: 我想知道Java是怎么做到的?它实际上是否在程序存储器中寻找一个String值并检查其是否存在,如果不存在则创建一