【问题标题】:extract variable from string variable using data.table and self-defined function使用 data.table 和自定义函数从字符串变量中提取变量
【发布时间】:2020-11-14 10:10:37
【问题描述】:
# simulate data
id=c(1:30)
house_type=sample(c("3STR","2STR","1STR","DETC,1STR","2STR,DETC","OTHERS"),20, replace=TRUE)
house=data.table(id,house_type)

# I want to create a house_type2 variable in house data.table
# In the real data, there are many other house type, with 1/2/3STR embeded in these house_typ. 
# I want to extract the STR (how many story) information from the data.

house[,story:="others"][str_detect("3STR",house_type),story:="3STR"][
  str_detect("2STR",house_type),story:="2STR"][
    str_detect("1STR",house_type),story:="1STR"]

模拟数据看起来很样本;在实际数据中,house_type 字符串有更多的变化。我设法用 data.table 链接语法做到了。但我要求一种更优雅的方式来做到这一点:定义一个函数来提取故事并在 house_type 列上应用函数。

【问题讨论】:

    标签: r string data.table


    【解决方案1】:

    您可以使用正则表达式来提取数字:

    library(data.table)
    library(stringr)
    # simulate data
    set.seed(123L)
    id=c(1:30)
    house_type=sample(c("3STR","2STR","1STR","DETC,1STR","2STR,DETC","OTHERS"),20, replace=TRUE)
    house=data.table(id,house_type)
    #> Warning in as.data.table.list(x, keep.rownames = keep.rownames, check.names
    #> = check.names, : Item 2 has 20 rows but longest item has 30; recycled with
    #> remainder.
    
    #OP
    house[,story:="others"][str_detect("3STR",house_type),story:="3STR"][
        str_detect("2STR",house_type),story:="2STR"][
            str_detect("1STR",house_type),story:="1STR"]
    
    ## Solutions
    house[, story2 := as.integer(sub("\\D*(\\d+).*", "\\1", house_type))]
    #> Warning in eval(jsub, SDenv, parent.frame()): NAs introduced by coercion
    house[, story3 := fifelse(is.na(story2), "other", paste0(story2, "STR"))]
    
    house
    #>        id house_type  story story2 story3
    #>     <int>     <char> <char>  <int> <char>
    #>  1:     1       1STR   1STR      1   1STR
    #>  2:     2     OTHERS others     NA  other
    #>  3:     3       1STR   1STR      1   1STR
    #>  4:     4       2STR   2STR      2   2STR
    #>  5:     5       2STR   2STR      2   2STR
    #>  6:     6     OTHERS others     NA  other
    #>  7:     7       1STR   1STR      1   1STR
    #>  8:     8  2STR,DETC others      2   2STR
    #>  9:     9  DETC,1STR others      1   1STR
    #> 10:    10     OTHERS others     NA  other
    #> 11:    11     OTHERS others     NA  other
    #> 12:    12       3STR   3STR      3   3STR
    #> 13:    13       2STR   2STR      2   2STR
    #> 14:    14       1STR   1STR      1   1STR
    #> 15:    15  2STR,DETC others      2   2STR
    #> 16:    16       1STR   1STR      1   1STR
    #> 17:    17       1STR   1STR      1   1STR
    #> 18:    18       3STR   3STR      3   3STR
    #> 19:    19  DETC,1STR others      1   1STR
    #> 20:    20       3STR   3STR      3   3STR
    #> 21:    21       1STR   1STR      1   1STR
    #> 22:    22     OTHERS others     NA  other
    #> 23:    23       1STR   1STR      1   1STR
    #> 24:    24       2STR   2STR      2   2STR
    #> 25:    25       2STR   2STR      2   2STR
    #> 26:    26     OTHERS others     NA  other
    #> 27:    27       1STR   1STR      1   1STR
    #> 28:    28  2STR,DETC others      2   2STR
    #> 29:    29  DETC,1STR others      1   1STR
    #> 30:    30     OTHERS others     NA  other
    #>        id house_type  story story2 story3
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 2021-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-04
      相关资源
      最近更新 更多