gpt4 book ai didi

regex - 如何将列添加到基于另一列中的字符串的 R 中的 data.table?

转载 作者:行者123 更新时间:2023-12-05 00:27:04 25 4
gpt4 key购买 nike

我想根据另一列中的字符串将列添加到 data.table。这是我的数据和我尝试过的方法:

                                                                                     Params1:                                   { clientID : 459;  time : 1386868908703;  version : 6}2: { clientID : 459;  id : 52a9ea8b534b2b0b5000575f;  time : 1386868824339;  user : 459001}3:                                                 { clientID : 988;  time : 1388939739771}4: { clientID : 459;  id : 52a9ec00b73cbf0b210057e9;  time : 1386868810519;  user : 459001}5:                                                 { clientID : 459;  time : 1388090530634}

Code to create this table:

DT = data.table(Params=c("{ clientID : 459;  time : 1386868908703;  version : 6}","{ clientID : 459;  id : 52a9ea8b534b2b0b5000575f;  time : 1386868824339;  user : 459001}","{ clientID : 988;  time : 1388939739771}","{ clientID : 459;  id : 52a9ec00b73cbf0b210057e9;  time : 1386868810519;  user : 459001}","{ clientID : 459;  time : 1388090530634}"))

我想解析“Params”列中的文本并根据其中的文本创建新列。例如,我想要一个名为“user”的新列,它只包含 Params 字符串中“user:”之后的数字。添加的列应如下所示:
                                                                                     Params          user1:                                   { clientID : 459;  time : 1386868908703;  version : 6} NA2: { clientID : 459;  id : 52a9ea8b534b2b0b5000575f;  time : 1386868824339;  user : 459001} 4590013:                                                 { clientID : 988;  time : 1388939739771} NA4: { clientID : 459;  id : 52a9ec00b73cbf0b210057e9;  time : 1386868810519;  user : 459001} 4590015:                                                 { clientID : 459;  time : 1388090530634} 459001

I created the following function to parse (in this case for the "user"):

myparse <- function(searchterm, s) {
s <-gsub("{","",s, fixed = TRUE)
s <-gsub(" ","",s, fixed = TRUE)
s <-gsub("}","",s, fixed = TRUE)
s <-strsplit(s, '[;:]')
s <-unlist(s)
if (length(s[which(s==searchterm)])>0) {s[which(s==searchterm)+1]} else {NA}
}

然后我使用以下函数添加一列:
DT <- transform(DT, user = myparse("user", Params))

这适用于包含在所有行中的“时间”,但不适用于仅包含在两行中的“用户”。返回以下错误:
Error in data.table(list(Params = c("{ clientID : 459;  time : 1386868908703;  version : 6}",  : 
argument 2 (nrow 2) cannot be recycled without remainder to match longest nrow (5)

我该如何解决这个问题?谢谢!

最佳答案

这是使用正则表达式完成此任务的一种方法:

myparse <- function(searchterm, s) {
res <- rep(NA_character_, length(s)) # NA vector
idx <- grepl(searchterm, s) # index for strings including the search term
pattern <- paste0(".*", searchterm, " : ([^;}]+)[;}].*") # regex pattern
res[idx] <- sub(pattern, "\\1", s[idx]) # extract target string
return(res)
}

您可以使用此函数添加新列,例如,对于 user :
DT[, user := myparse("user", Params)]

新列包含 NA对于没有 user 的行 field :
DT[, user]
# [1] NA "459001" NA "459001" NA

关于regex - 如何将列添加到基于另一列中的字符串的 R 中的 data.table?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21280730/

25 4 0