如果我在foreach... %dopar%没有注册集群的情况下运行,foreach会发出警告,并按顺序执行代码:
library("doParallel")
foreach(i=1:3) %dopar%
sqrt(i)
Run Code Online (Sandbox Code Playgroud)
产量:
Warning message:
executing %dopar% sequentially: no parallel backend registered
Run Code Online (Sandbox Code Playgroud)
但是,如果我在启动,注册和停止集群后运行相同的代码,则会失败:
cl <- makeCluster(2)
registerDoParallel(cl)
stopCluster(cl)
rm(cl)
foreach(i=1:3) %dopar%
sqrt(i)
Run Code Online (Sandbox Code Playgroud)
产量:
Error in summary.connection(connection) : invalid connection
Run Code Online (Sandbox Code Playgroud)
有没有相反的registerDoParallel()清理群集注册?还是我坚持使用旧集群的鬼魂,直到我重新开始我的R会话?
/编辑:一些谷歌搜索揭示bumphunter:::foreachCleanup()了bumphunter Biocondoctor包中的功能:
function ()
{
if (exists(".revoDoParCluster", where = doParallel:::.options)) {
if (!is.null(doParallel:::.options$.revoDoParCluster))
stopCluster(doParallel:::.options$.revoDoParCluster)
remove(".revoDoParCluster", envir = doParallel:::.options)
}
}
<environment: namespace:bumphunter>
Run Code Online (Sandbox Code Playgroud)
但是,此功能似乎无法解决问题.
library(bumphunter)
cl <- makeCluster(2)
registerDoParallel(cl)
stopCluster(cl)
rm(cl)
bumphunter:::foreachCleanup()
foreach(i=1:3) %dopar%
sqrt(i)
Run Code Online (Sandbox Code Playgroud)
foreach在哪里保留注册集群的信息?
我在R中有一个数据表:
name date
---- ----
John 1156649280
Adam 1255701960
...etc...
Run Code Online (Sandbox Code Playgroud)
我想得到日期在一个范围内的所有行.在SQL中,我可能会说SELECT * FROM mytable WHERE date > 5 AND date < 15
R中的等价物是什么,根据特定列中的值范围选择行?
我已经在各种机器上运行了一些代码测试代码,总是得到相同的结果.我认为各种do ...包背后的哲学是它们可以互换地用作foreach的%dopar%的后端.为什么不是这样?
例如,此代码段有效:
library(plyr)
library(doMC)
registerDoMC()
x <- data.frame(V= c("X", "Y", "X", "Y", "Z" ), Z = 1:5)
ddply(x, .(V), function(df) sum(df$Z),.parallel=TRUE)
Run Code Online (Sandbox Code Playgroud)
虽然这些代码段中的每一个都失败了:
library(plyr)
library(doSMP)
workers <- startWorkers(2)
registerDoSMP(workers)
x <- data.frame(V= c("X", "Y", "X", "Y", "Z" ), Z = 1:5)
ddply(x, .(V), function(df) sum(df$Z),.parallel=TRUE)
stopWorkers(workers)
library(plyr)
library(snow)
library(doSNOW)
cl <- makeCluster(2, type = "SOCK")
registerDoSNOW(cl)
x <- data.frame(V= c("X", "Y", "X", "Y", "Z" ), Z = 1:5)
ddply(x, .(V), function(df) sum(df$Z),.parallel=TRUE)
stopCluster(cl)
library(plyr)
library(doMPI)
cl <- startMPIcluster(count = 2) …Run Code Online (Sandbox Code Playgroud) 我有一个函数,我试图用roxygen2记录:
#' Name of function
#'
#' Description
#'
#' @param x The input data
#' @param method one of:
#' "method1" - very long text here
#' "method2" - very long text here
#' "method3" - very long text here
#' "method4" - very long text here
#' "method5" - very long text here
#' "method6" - very long text here
#' "method7" - very long text here
#' "method8" - very long text here
#' "method9" - very …Run Code Online (Sandbox Code Playgroud) R有is.vector,is.list,is.integer,is.double,is.numeric,is.factor,is.character,等为什么没有is.POSIXct,is.POSIXlt还是is.Date?
我需要一种可靠的方法来检测POSIXct物体,class(x)[1] == "POSIXct"看起来真的......很脏.
我在R中使用'agrep'函数,它返回一个匹配向量.我想要一个类似于agrep的函数,它只返回最佳匹配,或者如果有关系则返回最佳匹配.目前,我正在使用包'cba'中的'sdist()'函数对结果向量的每个元素执行此操作,但这似乎非常多余.
/ edit:这是我目前正在使用的功能.我想加快速度,因为计算距离两次似乎是多余的.
library(cba)
word <- 'test'
words <- c('Teest','teeeest','New York City','yeast','text','Test')
ClosestMatch <- function(string,StringVector) {
matches <- agrep(string,StringVector,value=TRUE)
distance <- sdists(string,matches,method = "ow",weight = c(1, 0, 2))
matches <- data.frame(matches,as.numeric(distance))
matches <- subset(matches,distance==min(distance))
as.character(matches$matches)
}
ClosestMatch(word,words)
Run Code Online (Sandbox Code Playgroud) 我有一个由我的控制之外的进程生成的大型data.frame,它可能包含也可能不包含方差为零的变量(即所有观察结果都相同).我想基于这些数据建立一个预测模型,显然这些变量是没用的.
这是我目前用来从data.frame中删除这些变量的函数.它目前基于apply,我想知道是否有任何明显的方法来加速这个功能,以便它可以在非常大的数据集上快速工作,具有大量(400或500)变量?
set.seed(1)
dat <- data.frame(
A=factor(rep("X",10),levels=c('X','Y')),
B=round(runif(10)*10),
C=rep(10,10),
D=c(rep(10,9),1),
E=factor(rep("A",10)),
F=factor(rep(c("I","J"),5)),
G=c(rep(10,9),NA)
)
zeroVar <- function(data, useNA = 'ifany') {
out <- apply(data, 2, function(x) {length(table(x, useNA = useNA))})
which(out==1)
}
Run Code Online (Sandbox Code Playgroud)
以下是该过程的结果:
> dat
A B C D E F G
1 X 3 10 10 A I 10
2 X 4 10 10 A J 10
3 X 6 10 10 A I 10
4 X 9 10 10 A J 10
5 X 2 10 …Run Code Online (Sandbox Code Playgroud) 该fastmatch包实现的更快版本match的重复匹配(例如,在一个循环中):
set.seed(1)
library(fastmatch)
table <- 1L:100000L
x <- sample(table, 10000, replace=TRUE)
system.time(for(i in 1:100) a <- match(x, table))
system.time(for(i in 1:100) b <- fmatch(x, table))
identical(a, b)
Run Code Online (Sandbox Code Playgroud)
是否有类似的实现%in%可用于加速重复查找?
每当我尝试在R中安装软件包时,都会收到以下错误:
Error in readRDS(file) : unknown input format
Run Code Online (Sandbox Code Playgroud)
这刚刚在系统崩溃后开始发生.我在Windows 7下运行32位R 2.13.0.我尝试删除并重新安装R,但继续得到错误.有没有办法解决这个问题而不删除所有内容(即我安装的所有软件包)并重新开始?
谢谢
我正在尝试让内联包在我的macbook上运行.以下代码块(来自cxxfunction示例)失败:
library(inline)
fx <- cxxfunction( signature(x = "integer", y = "numeric" ) , '
return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;
' )
fx( 2L, 5 )
Run Code Online (Sandbox Code Playgroud)
出现此错误:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! make: g++-4.2: No such file or directory
make: *** [file141b5882.o] Error 1
Run Code Online (Sandbox Code Playgroud)
这很奇怪,因为g ++可用:
g++ -v
Using built-in specs.
Target: i686-apple-darwin11
Configured with: /private/var/tmp/llvmgcc42/llvmgcc42-2336.1~22/src/configure --disable-checking --enable-werror --prefix=/Developer/usr/llvm-gcc-4.2 --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-prefix=llvm- --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin11 --enable-llvm=/private/var/tmp/llvmgcc42/llvmgcc42-2336.1~22/dst-llvmCore/Developer/usr/local …Run Code Online (Sandbox Code Playgroud)