R 使用data.table列中的参数按引用更新
我有以下数据表和函数,用于提取参数并将其作为列添加到数据表中:R 使用data.table列中的参数按引用更新,r,data.table,R,Data.table,我有以下数据表和函数,用于提取参数并将其作为列添加到数据表中: library(stringr) library(data.table) pq <- c("cm_mmc=MSN-_-Exact-_-BrandExact-_-CheddarCheese" ,"cm_mmc=Google-_-cheeseTypes-_-cheddar-_-cheesedelivered&gclid=CMXyy2D81MsCFcsW0w0dMVoPGw" ,"cm_mmc=MSN-_-worl
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]]
}
它创建列,但内容为空。我认为数据表源参数中的语法有问题。我缺少什么?将函数中的代码从
dt[,(col_name):= str_match((source), regexp)[,2]]
到
dt[,(col_name):= str_match((source), regexp)[,2]]
dt[,(col_name):= str_match(get(source), regexp)[,2]]