0
使用data.table柱更新內參數I具有以下數據表和函數,其提取的參數,並增加了它作爲數據表中的列:通過引用
library(stringr)
library(data.table)
pq <- c("cm_mmc=MSN-_-Exact-_-BrandExact-_-CheddarCheese"
,"cm_mmc=Google-_-cheeseTypes-_-cheddar-_-cheesedelivered&gclid=CMXyy2D81MsCFcsW0w0dMVoPGw"
,"cm_mmc=MSN-_-worldcitiesuslocations-_-cheese-_-cheeseshops"
,"cm_mmc=MSN-_-worldcitiesuslocations-_-cheese-_-cheeseshops")
rq <- c("q=cheese&src=IE-SearchBox&FORM=IESR02",
"sa=L",
"q=london+cheese+shop&src=IE-TopResult&FORM=IETR02&pc=WCUG",
"q=london+cheese+shop&src=IE-TopResult&FORM=IETR02&pc=WCUG")
DT = data.table(page_urlquery = pq, refr_urlquery = rq)
# Extracts a paramater from the relevant query and adds it to the table as a column
extract_param <- function(dt, source = c("page_urlquery", "refr_urlquery"), param_name){
source <- match.arg(source)
regexp <- paste("(?i)", param_name, "=([^&]+)", sep="")
col_name <- switch(source
,"page_urlquery" = paste("url_", param_name, sep = "")
,"refr_urlquery" = paste("ref_", param_name, sep = "")
)
dt[,(col_name):= str_match((source), regexp)[,2]]
}
然而,當我調用函數作爲如下:
extract_param(DT, "page_urlquery", "cm_mmc")
它創建列,但內容是空白的。我認爲它在數據表(源)參數中的語法有問題。我錯過了什麼?