【问题标题】:How to write a function around data.table::tstrsplit() to separate strings into different columns?如何围绕 data.table::tstrsplit() 编写一个函数来将字符串分成不同的列?
【发布时间】:2021-09-24 05:32:22
【问题描述】:

这个问题可能是重复的,但我没有找到解决方案,所以提前道歉。 我想构建一个更快的替代tidyr::separate(),它将一列中的字符串分成新列。我决定使用data.table 来完成这项任务。

我创建了以下函数:

library(data.table)
library(tibble)

fast_separate <- function(.data, origin_col, into) {
  
  my_origin_col <- deparse(substitute(origin_col))
  
  my_dt <- as.data.table(.data)
  my_dt <- my_dt[, into := tstrsplit(my_origin_col, "_", fixed = TRUE)][] # https://rdatatable.gitlab.io/data.table/reference/tstrsplit.html
  as_tibble(my_dt)
}

我的功能失败
假设我有以下玩具数据:

df_toms <- tribble(~toms, 
              "tom_hanks",
              "tom_bradey",
              "tom_cruise",
              "thomas_edison",
              "thomas_fefferson"
              )

df_toms
## # A tibble: 5 x 1
##   toms            
##   <chr>           
## 1 tom_hanks       
## 2 tom_bradey      
## 3 tom_cruise      
## 4 thomas_edison   
## 5 thomas_fefferson

当我打电话给fast_separate() 我得到:

fast_separate(df_toms, origin_col = toms, into = c("first_name", "surname"))

## # A tibble: 5 x 2
##   toms             into 
##   <chr>            <chr>
## 1 tom_hanks        toms 
## 2 tom_bradey       toms 
## 3 tom_cruise       toms 
## 4 thomas_edison    toms 
## 5 thomas_fefferson toms 

这不是所需的输出。
这很奇怪,因为我们会定期运行相同的代码(即不在函数内部):

my_dt <- as.data.table(df_toms)
my_dt <- my_dt[, c("first_name", "surname") := tstrsplit(toms, "_", fixed = TRUE)][]
desired_output <- as_tibble(my_dt)

desired_output 
## # A tibble: 5 x 3
##   toms             first_name surname  
##   <chr>            <chr>      <chr>    
## 1 tom_hanks        tom        hanks    
## 2 tom_bradey       tom        bradey   
## 3 tom_cruise       tom        cruise   
## 4 thomas_edison    thomas     edison   
## 5 thomas_fefferson thomas     fefferson

我写fast_separate()的方式有什么问题?

【问题讨论】:

    标签: r string dataframe function data.table


    【解决方案1】:

    这提供了所需的输出

    library(data.table)
    library(tibble)
    fast_separate <- function(.data, origin_col, into) {
      as.tibble(setDT(.data)[, (into) := tstrsplit(.data[[origin_col]], "_", fixed = TRUE)])
    }
    
    df_toms <- tribble(~toms, 
                       "tom_hanks",
                       "tom_bradey",
                       "tom_cruise",
                       "thomas_edison",
                       "thomas_fefferson"
    )
    
    
    fast_separate(df_toms, origin_col = "toms", into = c("first_name", "surname"))
    # # A tibble: 5 x 3
    #   toms             first_name surname  
    #   <chr>            <chr>      <chr>    
    # 1 tom_hanks        tom        hanks    
    # 2 tom_bradey       tom        bradey   
    # 3 tom_cruise       tom        cruise   
    # 4 thomas_edison    thomas     edison   
    # 5 thomas_fefferson thomas     fefferson
    

    Thomas Fefferson 是谁? ;-)

    【讨论】:

    • 这是完美的,谢谢。您是否有使用data.table 编程的参考(例如,手册、插图或其他)?例如,在将字符向量传递给函数时,我没有看到任何需要在 () 中包装字符向量的地方?
    • (vector) 的解释在?:= 的帮助中有所描述,也在这里:cran.r-project.org/web/packages/data.table/vignettes/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-04
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-10
    • 1970-01-01
    相关资源
    最近更新 更多