我有一个30万行数据帧,其中的列是这样的:
db$performance[1:10]
[1] "1C1CCCCCCCCCCCCCCCCCCCCCC" "CCCCCCCCCCCCC"
"4321CCCCCCCCCCCCCCCCCCCCC" "321CCCCCCCCCCCCCCCCCCCCCC"
[5] "CCCCCCCCCCCCCC" "4321CCCCCCCCCCCCC0" "211CCCCCCCCCCCCCCCCCCCCCC" "BCCCCCCCCC" [9] "BCCCCCCCCC" "8"
我想搜索该列的每一行并计算最后18个(从右到左)字符元素中出现的“ 4”个数字。我拥有的循环解决方案显然很糟糕,因为它非常慢(6分钟或更长时间)。见下文。我如何向量化解决方案(使用apply和/或dplyr?)
谢谢!
substrRight <- function(x, n){
substr(x, nchar(x)-n, nchar(x))
}
db$NewVar = NA
for (N in 1:nrow(db)){
db$NewVar[N] = str_count( substrRight(db$performance[N],18), "4")
}
答案 0 :(得分:4)
str_count
和substr
已经向量化。因此,直接将功能应用于整个列
library(stringr)
str_count(substrRight(db$performance, 18), "4")
#[1] 0 0 0 0 0 1 0 0 0 0
它应该足够快。检查更大数据集上的时间
db1 <- db[rep(seq_len(nrow(db)), 1e5),, drop = FALSE]
system.time({
out <- numeric(nrow(db1))
for (i in seq_len(nrow(db1))){
out[i]= str_count( substrRight(db1$performance[i],18), "4")
}
})
# user system elapsed
# 14.699 0.104 14.755
system.time({
sapply(db1$performance, function(x) str_count( substrRight(x,18), "4") )
})
# user system elapsed
# 14.267 0.075 14.299
system.time({
str_count(substrRight(db1$performance, 18), "4")
})
# user system elapsed
# 0.437 0.016 0.452
db <- structure(list(performance = c("1C1CCCCCCCCCCCCCCCCCCCCCC", "CCCCCCCCCCCCC",
"4321CCCCCCCCCCCCCCCCCCCCC", "321CCCCCCCCCCCCCCCCCCCCCC", "CCCCCCCCCCCCCC",
"4321CCCCCCCCCCCCC0", "211CCCCCCCCCCCCCCCCCCCCCC", "BCCCCCCCCC",
"BCCCCCCCCC", "8")), class = "data.frame", row.names = c(NA,
-10L))