【发布时间】:2020-01-17 09:53:52
【问题描述】:
我有一个 filter() 函数,用于过滤 tibble 中的特定正则表达式。因为我需要多次执行此操作,所以我想编写一个 as_mapper() 函数以得到更短的代码。我该怎么做?
我尝试了以下方法:
adverts <- as_mapper(~!grepl("(xtm)|((k|K)(i|I|1|11)(d|D)(n|N).)|(Ar<e)\\s(you)\\s(in)|
(LOAN)|(AR(\\s|\\S)[0-9])|((B|b)(i|1|l)tc.)|(Coupon)|(Plastic.King)|(organs)|(SILI)|(Electric.Cigarette.Machine)",.$value,perl = T)%>% filter)
如果我尝试将此函数添加到 tibble,R 会抛出 C stack usage x is too close to the limit 错误。我怎样才能避免这种情况?
可以使用以下代码生成我要检查的 tibble 之一:
library(tidyverse)
library(rvest)
library(textreadr)
bribe <- read_html(paste("http://ipaidabribe.com/reports/paid?page", 10, sep = "="))
all.nodes <- c(".heading-3 a",".paid-amount span", ".date", ".location", ".transaction a")
test <- map(all.nodes, ~ html_nodes(bribe, .x) %>% html_text()) %>%
unlist %>%
as_tibble
adverts(test)
【问题讨论】: