R: 你如何在 lapply() 中应用 grep()

R: How do you apply grep() in lapply()

我想在 R 中应用 grep(),但我不太擅长 lapply()。我知道 lapply 能够获取一个列表,将函数应用于每个成员并输出一个列表。例如,设 x 是一个由 2 个成员组成的列表。

> x<-strsplit(docs$Text," ")
> 
> x
[[1]]
 [1] "I"         "lovehttp"  "my"        "mum."      "I"         "love"     
 [7] "my"        "dad."      "I"         "love"      "my"        "brothers."

[[2]]
 [1] "I"         "live"      "in"        "Eastcoast" "now."      "Job.I"    
 [7] "used"      "to"        "live"      "in"        "WestCoast."  

我想应用 grep() 函数来删除由 http 组成的单词。所以,我会申请:

> lapply(x,grep(pattern="http",invert=TRUE, value=TRUE))

但它不起作用,上面写着

Error in grep(pattern = "http", invert = TRUE, value = TRUE) : 
argument "x" is missing, with no default

所以,我试过了

> lapply(x,grep(pattern="http",invert=TRUE, value=TRUE,x))

但是它说

Error in match.fun(FUN) : 
'grep(pattern = "http", invert = TRUE, value = TRUE, x)' is not a 
function, character or symbol

求助,谢谢!

以下代码行将从列表中的向量中删除所有包含子字符串 http:

的条目
repx <- function(x) {
    y <- grep("http", x)
    vec <- rep(TRUE, length(x))
    vec[y] <- FALSE
    x <- x[vec]
    return(x)
}

lapply(lst, function(x) { repx(x) })

数据:

x1 <- c("I", "lovehttp", "my", "mum.", "I", "love", "my", "dad.", "I", "love", "my", "brothers.")
x2 <- c("I", "live", "in", "Eastcoast", "now.", "Job.I", "used", "to", "live", "in", "WestCoast.")
lst <- list(x1, x2)

这可以在一行中完成:

lst <- lapply(lst, grep, pattern="http", value=TRUE, invert=TRUE)

#lst
#[[1]]
# [1] "I"         "my"        "mum."      "I"         "love"      "my"        "dad."      "I"         "love"      "my"        "brothers."
#
#[[2]]
# [1] "I"          "live"       "in"         "Eastcoast"  "now."       "Job.I"      "used"       "to"         "live"       "in"         "WestCoast."

如果您不想删除包含该模式的整个单词并仅删除该模式本身同时保留该单词的其余部分(如评论中所述),您可以使用 gsub 代替共 grep 个:

lapply(lst, gsub, pattern="http", replacement="")
#[[1]]
# [1] "I"         "love"      "my"        "mum."      "I"         "love"      "my"        "dad."      "I"         "love"      "my"        "brothers."
#
#[[2]]
# [1] "I"          "live"       "in"         "Eastcoast"  "now."       "Job.I"      "used"       "to"         "live"       "in"         "WestCoast."