【问题标题】:Add a column on a dataframe在数据框上添加一列
【发布时间】:2019-01-17 21:00:16
【问题描述】:

如果你能帮忙,我有一个 R 问题。

x <- data.frame("LocationCode" = c("ESC3","RIECAA6","SJHMAU","RIE104","SJH11","SJHAE","RIEAE1","WGH54","RIE205","GSBROB"), "HospitalNumber" = c("701190923R","2905451068","700547389X","AN11295201","1204541612","104010665","800565884R","620063158W","600029720K","1112391223"),"DisciplineName" = c("ESC Biochemistry", "RIE Haematology","SJH Biochemistry","RIE Biochemistry","SJH Biochemistry","WGH Biochemistry","ESC Biochemistry","WGH Biochemistry","SJH Biochemistry","RIE Haematology"))

从上面的数据框中,我确实希望添加一个由所有“HospitalNumber”行组成的新列 (CRN),其中 9 位数字加上末尾的 1 个字母(例如 701190923R),创建另一列 (TIT),其余部分不符合第一个条件的行

【问题讨论】:

    标签: r


    【解决方案1】:

    您可以使用代码在 base 中执行此操作

    # Identify cases which match 9 digits then one letter
    CRMMatch <- grepl("^\\d{9}[[:alpha:]]$", as.character(x$HospitalNumber))
    #Create columns from Hospital number among the matches or those that do not match
    x$CRN[CRMMatch] <- as.character(x$HospitalNumber)[CRMMatch]
    x$TIT[!CRMMatch] <- as.character(x$HospitalNumber)[!CRMMatch]
    # clean up by removing the variable created of matches
    rm(CRMMatch)
    

    dplyr 版本可能是

    library(dplyr)
    x <-
      x %>% 
      mutate(CRN = if_else(grepl("^\\d{9}[[:alpha:]]$", as.character(HospitalNumber)),as.character(HospitalNumber), NA_character_),
             TIT = if_else(!grepl("^\\d{9}[[:alpha:]]$", as.character(HospitalNumber)),as.character(HospitalNumber), NA_character_))
    

    【讨论】:

      【解决方案2】:

      您可以根据说明检测您需要的内容

      library(stringr)
      str_which(x$HospitalNumber,"[:digit:][:alpha:]")
      

      你会得到:

      > str_which(x$HospitalNumber,"[:digit:][:alpha:]")
      [1] 1 3 7 8 9
      

      然后你就知道你需要什么职位,你不需要什么

      【讨论】:

        【解决方案3】:

        与 Kerry Jackson 的方法非常相似,但在基础 R 中使用 ifelse。我还从一开始就将您的 x$HospitalNumber 从因子转换为字符,假设这是您真正想要的:

        x[2] <- as.character( x[ , 2 ] )
        x$CRN <- ifelse( grepl( "^\\d{9}[[:alpha:]]$", x$HospitalNumber) , x$HospitalNumber, "" )
        x$TIT <- ifelse( x$CRN != "", "", x$HospitalNumber )
        

        给你

        > x
           LocationCode HospitalNumber   DisciplineName        CRN        TIT
        1          ESC3     701190923R ESC Biochemistry 701190923R           
        2       RIECAA6     2905451068  RIE Haematology            2905451068
        3        SJHMAU     700547389X SJH Biochemistry 700547389X           
        4        RIE104     AN11295201 RIE Biochemistry            AN11295201
        5         SJH11     1204541612 SJH Biochemistry            1204541612
        6         SJHAE      104010665 WGH Biochemistry             104010665
        7        RIEAE1     800565884R ESC Biochemistry 800565884R           
        8         WGH54     620063158W WGH Biochemistry 620063158W           
        9        RIE205     600029720K SJH Biochemistry 600029720K           
        10       GSBROB     1112391223  RIE Haematology            1112391223
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-05-17
          • 2018-12-17
          • 2021-06-11
          • 1970-01-01
          • 2017-07-19
          • 1970-01-01
          相关资源
          最近更新 更多