R 特定命令的进度行
有这样的代码:R 特定命令的进度行,r,sapply,R,Sapply,有这样的代码: df <- data.frame(text = c("I love to travel to London", "Germany was a fun country to visit."), stringAsFactors = FALSE) replace_cities_countries <- function(string, replacement) { library(maps) data(world
df <- data.frame(text = c("I love to travel to London",
"Germany was a fun country to visit."), stringAsFactors = FALSE)
replace_cities_countries <- function(string, replacement) {
library(maps)
data(world.cities)
patterns <- unique(c(world.cities$name, world.cities$country.etc))
for (i in seq_along(patterns))
string <- gsub(patterns[i], replacement, string, perl=TRUE)
string
}
sapply(df$text, replace_cities_countries, replacement='HOORAY!')
df可能使用
df <- data.frame(text = c("I love to travel to London",
"Germany was a fun country to visit."), stringsAsFactors = FALSE)
counter <- 0
max_count <- length(df$text)
my_function <- function(x) {
counter <<- counter + 1 # Note the double <<
cat("Iter", counter, "/", max_count, "\n")
return(nchar(x))
}
> out <- sapply(df$text, my_function)
Iter 1 / 2
Iter 2 / 2
> out
I love to travel to London Germany was a fun country to visit.
26 35
> length(out)
[1] 2