首页 > 解决方案 > 如何使用 case_when 从列表中删除元素

问题描述

我有一个嵌套的字符串列表,我想在满足 grepl 条件时在新列中输入一个值,并且我还想从嵌套列表中删除该元素

我的列表如下(嵌套在数据框中)

list( "Normal Mucosa Throughout", "Mitotic Lesion- See Text Above", 
    "Normal", 
    c("Mitotic", "Hiatus Hernia"), "Normal Mucosa Throughout", 
    "HALO RFA to Barrett's oesophagus", "Barretts oesophagus", 
    c("Barrett's oesophagus ", "EMR"))

期望的结果:

FindingsAfterProcessing                  DiagnosisCode             
Normal Mucosa Throughout                 other  
                                         C159
Normal                                   other
 Hiatus Hernia                           C159
Normal Mucosa Throughout                 other
HALO RFA to Barrett's oesophagus         other
Barretts oesophagus                      other
Barrett's oesophagus                     other
                                         EMR

我的case_when

 myDf<-myDf%>%   
    mutate(DiagnosisCode             = case_when( 
        grepl("mitotic|emr",tolower(myDf$OriginalFindings),ignore.case=TRUE) ~  "C159  -  Malignant neoplasm oesophagus, unspecified - Oesophagus - unspecified",
        TRUE                      ~  "other")
    )

如何将此应用于嵌套列表并在找到后删除该元素?

标签: r

解决方案


尝试mappurrr(你也可以只加载tidyverse):

library(tidyverse)

df %>%
  mutate(
    DiagnosisCode = map(
      FindingsAfterProcessing, ~ case_when(
        any(grepl("mitotic", tolower(.x))) ~ "C159",
        any(grepl("emr", tolower(.x))) ~ "EMR",
        TRUE ~ "other")
      ),
    FindingsAfterProcessing = map(
      FindingsAfterProcessing, ~ .x[!grepl("mitotic|emr", tolower(.x))]
      )
  )

这里的输出是:

           FindingsAfterProcessing DiagnosisCode
1         Normal Mucosa Throughout         other
2                                           C159
3                           Normal         other
4                    Hiatus Hernia          C159
5         Normal Mucosa Throughout         other
6 HALO RFA to Barrett's oesophagus         other
7              Barretts oesophagus         other
8            Barrett's oesophagus            EMR

这不是 100% 对应于您想要的输出,但我猜您的输出中有错字?

我这样说是因为我的输出是基于您提供的列表;我已经把它变成了一个数据框列:

df <- data.frame(FindingsAfterProcessing = I(list( "Normal Mucosa Throughout", "Mitotic Lesion- See Text Above", 
                                  "Normal", 
                                  c("Mitotic", "Hiatus Hernia"), "Normal Mucosa Throughout", 
                                  "HALO RFA to Barrett's oesophagus", "Barretts oesophagus", 
                                  c("Barrett's oesophagus ", "EMR"))))

推荐阅读