取消列出数据框列并将它们粘贴在一起

时间:2017-07-12 04:55:50

标签: r dplyr tidyr stringr tidyverse

我有一个如下定义的数据框:

df <- structure(list(ID = 1:19, MEDICATION = c("0", "NOVOMIX 26 BF, 20 D", 
                                               "NOVOMIX 14 D", "NOVOMIX 34 BF 22 D", "MIXTARD 52 BF 20 D", "MIXTARD 40 BF 24 D", 
                                               "MIXTARD 10 BF 8 D", "MIXTARD 42 BF 24 D", "MIXTARD 20 BF 18 D", 
                                               "MIXTARD 82 BF 46 D", "MIXTARD 14 BF 10 D", "NOVOMIX 15 BF 15 D", 
                                               "MIXTARD", NA, "MIXTARD 10 BF 4 D", "NOVOMIX", "MIXTARD --> NOVOMIX", 
                                               "NOT GIVEN ANY DIABETES MEDICATION INPATIENT PATIENT NORMALLY ON METFORMIN", 
                                               "GIVEN ASPART")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -19L), .Names = c("ID", "MEDICATION"))

我想从数据框中的NOVOMIX变量中提取所有药物(即MIXTARDMETFORMINASPARTMEDICATION并粘贴它们我编写了如下代码:

library(tidyverse)
library(rebus)
df %>%
      mutate(MEDICATION2 = str_extract_all(MEDICATION, pattern = 
                           or1(c("NOVOMIX", "MIXTARD", "METFORMIN", "ASPART")))) %>%
      unnest(MEDICATION2) %>%
      group_by(ID) %>%
      mutate(MEDICATION2 = str_c(unlist(MEDICATION2), collapse = " - ")) %>%
      slice(1)

我的预期输出是:

df_out <- structure(list(ID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
13, 14, 15, 16, 17, 18, 19), MEDICATION = c("0", "NOVOMIX 26 BF, 20 D", 
"NOVOMIX 14 D", "NOVOMIX 34 BF 22 D", "MIXTARD 52 BF 20 D", "MIXTARD 40 BF 24 D", 
"MIXTARD 10 BF 8 D", "MIXTARD 42 BF 24 D", "MIXTARD 20 BF 18 D", 
"MIXTARD 82 BF 46 D", "MIXTARD 14 BF 10 D", "NOVOMIX 15 BF 15 D", 
"MIXTARD", NA, "MIXTARD 10 BF 4 D", "NOVOMIX", "MIXTARD --> NOVOMIX", 
"NOT GIVEN ANY DIABETES MEDICATION INPATIENT PATIENT NORMALLY ON METFORMIN", 
"GIVEN ASPART"), MEDICATION2 = c(NA, "NOVOMIX", "NOVOMIX", "NOVOMIX", 
"MIXTARD", "MIXTARD", "MIXTARD", "MIXTARD", "MIXTARD", "MIXTARD", 
"MIXTARD", "NOVOMIX", "MIXTARD", NA, "MIXTARD", "NOVOMIX", "MIXTARD - NOVOMIX", 
"METFORMIN", "ASPART")), .Names = c("ID", "MEDICATION", "MEDICATION2"
), row.names = c(NA, -19L), class = "data.frame")

问题是代码删除了MEDICATION == 0行,我认为我的代码太长,无法简单地提取字符串。如果您知道如何缩短此代码(如果可能的话),我想请求帮助。

1 个答案:

答案 0 :(得分:3)

我们可以使用stri_extract_all_regex包中的stringi来提取与该模式匹配的所有字词。

library(stringi)
med_pattern <- c("NOVOMIX|MIXTARD|METFORMIN|ASPART")
df$MEDICATION2 <- stri_extract_all_regex(df$MEDICATION, pattern = med_pattern)

如@ mt1022所述,新列是一个列表。我们可以paste与他们一起

df$MEDICATION2<-paste(stri_extract_all_regex(df$MEDICATION,pattern = med_pattern)) 

但是,它不会为包含多个元素的列表提供一些不需要的字符。这应该会给你预期的输出。

chars <- stri_extract_all_regex(df$MEDICATION, pattern = med_pattern)
df$MEDICATION2 <- sapply(chars, paste, collapse = "-")
df$MEDICATION2

#[1] "NA"              "NOVOMIX"         "NOVOMIX"         "NOVOMIX"        
#[5] "MIXTARD"         "MIXTARD"         "MIXTARD"         "MIXTARD"        
#[9] "MIXTARD"         "MIXTARD"         "MIXTARD"         "NOVOMIX"        
#[13] "MIXTARD"         "NA"              "MIXTARD"         "NOVOMIX"        
#[17] "MIXTARD-NOVOMIX" "METFORMIN"       "ASPART" 

您也可以单行执行此操作:

df$MEDICATION2 <- sapply(stri_extract_all_regex(df$MEDICATION, 
                         pattern = med_pattern), paste, collapse = "-")