小编Mar*_*gan的帖子

为什么在具有多个组的大型数据帧上拆分效率低?

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)

performance r purrr

8
推荐指数
2
解决办法
549
查看次数

R编程:使用setMethod覆盖一组类的"summary"函数

我使用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)

这是有效的,但我想知道是否有更好的方法来实现我的目标,即为每个不同的类紧凑地创建一个"摘要"功能.

谢谢,

约翰.

r s4

5
推荐指数
1
解决办法
1492
查看次数

使用R中的trycatch抑制系统错误

我无法抑制错误,虽然我的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)

error-handling r try-catch

2
推荐指数
1
解决办法
2682
查看次数

标签 统计

r ×3

error-handling ×1

performance ×1

purrr ×1

s4 ×1

try-catch ×1