【问题标题】:extending a function that takes a data.table as an argument to use the full table (instead of a subset)扩展将 data.table 作为参数的函数以使用完整表(而不是子集)
【发布时间】:2016-05-30 16:43:11
【问题描述】:

我有一个函数适用于 1 行的 data.table (data.frame),但不适用于完整的 data.table。我想扩展该函数以考虑输入 data.table 的所有行。

论证的要点如下:

一个字段为字符串的data.table (tryshort3),需要用另一个data.table (mapping)中的另一个字符串替换,MRE如下:

#this is the original data.table
tryshort3 <- structure(list(country = c("AT", "AT", "MT", "DE", "CH", "XK"
), name = c("ASDF AG", "ASDF GMBH", "ASDF DF", "ASDF KG", "ASDF SA", 
"ASDF DAF"), address = c("ACDSTR. 3", "ACDSTR. 4", "ACDSTR. 5", 
"ACDSTR. 6", "ACDSTR. 7", "ACDSTR. 8")), .Names = c("country", 
"name", "address"), row.names = c(NA, -6L), class = c("data.table", 
"data.frame"))



#this is the "mapping
mapping <- structure(list(country = c("AT", "AT", "DE", "DE", "HU"), short.form = c("AG", 
"GMBH", "GMBH", "EV", "EV"), long.form = c("AKTIENGESELLSCHAFT", 
"GESELLSCHAFT MIT BESCHRANKTER HAFTUNG", "GESELLSCHAFT MIT BESCHRANKTER HAFTUNG", 
"EINGETRAGENE VEREIN", "EGYENI VALLALKOZO")), .Names = c("country", 
"short.form", "long.form"), row.names = c(NA, -5L), class = c("data.table", 
"data.frame"), sorted = "country")


#this is the function that I am using (please not that both data.tables are keyed, but that has currently no say in the output (just avoids throwing an error):

substituting_short_form <- function(input) {
  #supply one data.frame of 1 row, the other data.frame is external to the function
  #get country from input
  setkey(input,country)
  setkey(mapping,country)
  matched_country <- input$country
  #subset of mapping to only the country from the input
  matched_map <- mapping[country == matched_country]
  #get list of short.forms from matched 
  list_of_relevant_short_forms <- matched_map[,short.form]
  #which one matches will return true if there is any match, THIS IS A NUMBER THAT WILL HAVE TO BE MATCHED TO mapping again to retrieve the correct form
  #error catching for when there is no short form found, or no country found if there is no long form it does not matter!
  indextrue <- tryCatch(which(unlist(lapply(list_of_relevant_short_forms, function(y) grepl(y, input$name)))), error = function(e) return(input))
  #substitute
  pattern_to_substitute <- paste0("(\\s|^)", matched_map[indextrue,short.form], "(\\s|$)")
  pattern_to_replace <- paste0("\\1", matched_map[indextrue,long.form], "\\2")
  input$name[1] <- gsub(pattern = pattern_to_substitute, replacement = pattern_to_replace,input$name ,    perl = TRUE)
  return(input)
}

简而言之,这个函数的作用是将tryshort3作为输入(目前仅与tryshort3[1,]一起使用)并在“名称”字段中替换mapping表中的值,如下所示:

> tryshort3[1,]
   country    name   address
1:      AT ASDF AG ACDSTR. 3
> substituting_short_form(tryshort3[1,])
   country                    name   address
1:      AT ASDF AKTIENGESELLSCHAFT ACDSTR. 3

我想要什么,我提供完整的 data.table 作为输入,并获得相同的输出(相同长度的 data.table),这是我的预期输出:

   country                    name   address
1:      AT ASDF AKTIENGESELLSCHAFT ACDSTR. 3
2:      AT ASDF GESELLSCHAFT MIT BESCHRANKTER HAFTUNG ACDSTR. 4
3:      CH ASDF SA ACDSTR. 7
4:      DE ASDF KG ACDSTR. 6
5:      MT ASDF DF ACDSTR. 5
6:      XK ASDF DAF ACDSTR. 8

我想要的解决方案是来自函数 apply(tryshort3, 1, function(x) substituting_short_form(x) ) 的内容,可能使用两个 data.tables 的索引功能,或者可能使用 gapplyfrom nlme 从内部?

【问题讨论】:

    标签: r indexing data.table


    【解决方案1】:

    也许你可以分几个步骤尝试:

    # create the shortform variable in tryshort3
    tryshort3[, short.form := sub(".+\\s([^s]+)$", "\\1", name)]
    
    # add the info from mapping
    tryshort3long <- merge(tryshort3, mapping, all.x=TRUE, by=c("country", "short.form"))
    
    # replace the short form by long form in the name and suppress the variables you don't need 
    # (thanks to @DavidArenburg for the simplification of the "replace" part!)
    tryshort3long[!is.na(long.form), 
                  name := paste(sub(" .*", "", name), long.form)
                  ][, c("long.form", "short.form") := NULL]
    
    tryshort3long
       # country                                       name   address
    # 1:      AT                    ASDF AKTIENGESELLSCHAFT ACDSTR. 3
    # 2:      AT ASDF GESELLSCHAFT MIT BESCHRANKTER HAFTUNG ACDSTR. 4
    # 3:      CH                                    ASDF SA ACDSTR. 7
    # 4:      DE                                    ASDF KG ACDSTR. 6
    # 5:      MT                                    ASDF DF ACDSTR. 5
    # 6:      XK                                   ASDF DAF ACDSTR. 8
    

    NB:抱歉,我只是将它用于您的示例 data.table,而不是作为函数

    【讨论】:

    • 感谢@David! :-) 我感觉到有一种方法可以避免 ifelse ;-)
    【解决方案2】:

    apply 的问题在于它会将其参数强制转换为矩阵。尝试一个简单的循环:

    lst <- list()
    for(i in 1:nrow(tryshort3)) lst[[i]] <- substituting_short_form(tryshort3[i,])
    rbindlist(lst)
    #    country                                       name   address
    # 1:      AT                    ASDF AKTIENGESELLSCHAFT ACDSTR. 3
    # 2:      AT ASDF GESELLSCHAFT MIT BESCHRANKTER HAFTUNG ACDSTR. 4
    # 3:      MT                                    ASDF DF ACDSTR. 5
    # 4:      DE                                    ASDF KG ACDSTR. 6
    # 5:      CH                                    ASDF SA ACDSTR. 7
    # 6:      XK                                   ASDF DAF ACDSTR. 8
    

    【讨论】:

    • 这里没有读过任何东西,但如果你已经在运行for 循环,不妨看看set...
    猜你喜欢
    • 2016-09-25
    • 2021-06-13
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 2022-12-17
    • 2014-08-05
    • 2021-03-08
    • 2013-05-22
    相关资源
    最近更新 更多