Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(data.table)
- library(stringr)
- library(pipeR)
- library(zoo)
- # 產生資料
- numDigits <- 6
- numRows <- 1000
- DT2 <- data.table(str = rollapply(sample(9, numRows*numDigits, TRUE),
- numDigits, function(x) str_c(x, collapse = ""),
- by = numDigits), value = NA_character_)
- # 產生mapping table
- allPatterns <- substring(DT2$str,1,3) %>>% unique %>>% sort
- DT <- data.table(pattern = str_c("^", allPatterns, ".*"),
- value = sprintf("A%03i", 1:length(allPatterns)))
- # mapping開始
- st <- proc.time()
- for (i in 1:nrow(DT))
- set(DT2, which(str_detect(DT2$str, DT$pattern[i])),
- which(names(DT2) == "value"), DT$value[i])
- proc.time() - st
- # user system elapsed
- # 0.11 0.00 0.11
- print(DT2)
- # str value
- # 1: 588847 A297
- # 2: 472447 A225
- # 3: 181823 A048
- # 4: 928228 A495
- # 5: 331838 A139
- # ---
- # 996: 172326 A042
- # 997: 522373 A253
- # 998: 828978 A437
- # 999: 617415 A311
- # 1000: 877184 A470
Advertisement
Add Comment
Please, Sign In to add comment