df %>% split(.$x)
Run Code Online (Sandbox Code Playgroud)
对于大量x的唯一值变得缓慢.如果我们将数据帧手动拆分为较小的子集,然后对每个子集执行拆分,我们将时间减少至少一个数量级.
library(dplyr)
library(microbenchmark)
library(caret)
library(purrr)
N <- 10^6
groups <- 10^5
df <- data.frame(x = sample(1:groups, N, replace = TRUE),
y = sample(letters, N, replace = TRUE))
ids <- df$x %>% unique
folds10 <- createFolds(ids, 10)
folds100 <- createFolds(ids, 100)
Run Code Online (Sandbox Code Playgroud)
跑步microbenchmark
给了我们
## Unit: seconds
## expr mean
l1 <- df %>% split(.$x) # 242.11805
l2 <- lapply(folds10, function(id) df %>%
filter(x %in% id) %>% split(.$x)) %>% flatten # 50.45156
l3 <- lapply(folds100, function(id) df %>% …
Run Code Online (Sandbox Code Playgroud) 我使用setMethod覆盖不同类对象的'summary'函数.
最初,我使用了这种方法:
setMethod('summary', "class_1",
function(object, ...) {
#code for class_1 summary here...
}
)
setMethod('summary', "class_2",
function(object, ...) {
#code for class_2 summary here...
}
)
setMethod('summary', "class_3",
function(object, ...) {
#code for class_3 summary here...
}
)
Run Code Online (Sandbox Code Playgroud)
...等等每个班级.
但是,共有12个不同的类,因此代码变得非常重复.为了避免这种重复,我创建了一个包含类名的函数:
all_classes = function() {
c("class_1", "class_2", "class_3") #and so on for each class
}
Run Code Online (Sandbox Code Playgroud)
然后我用lapply:
lapply(
1:length(all_classes()),
function(k)
setMethod('summary', all_classes()[k],
function(object, ...) {
#code here...
}
)
)
Run Code Online (Sandbox Code Playgroud)
这是有效的,但我想知道是否有更好的方法来实现我的目标,即为每个不同的类紧凑地创建一个"摘要"功能.
谢谢,
约翰.
我无法抑制错误,虽然我的pdf文件正在转换为文本,但我无法抑制此错误.
tryCatch(system(paste("\"", exe, "\" \"", dest, "\"", sep = ""), wait = T))
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
> suppressWarnings(system(paste("\"", exe, "\" \"", dest, "\"", sep = ""), wait = T))
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
Syntax Error: Missing or …
Run Code Online (Sandbox Code Playgroud)