我真正喜欢的data.table是:=通过引用更改表格的习惯用语,而不需要昂贵的副本.据我所知,data.table与其他方法相比,这是超快的方面之一.
现在,我开始玩dplyr这个看似同样高效的包.但由于结果仍然需要使用<-运营商进行分配,因此我预计此级别的性能会下降.然而,似乎没有.
举个例子:
library(dplyr)
library(Lahman)
library(microbenchmark)
library(ggplot2)
df <- Batting[ c("yearID", "teamID", "G_batting") ]
mb <- microbenchmark(
dplyr = {
tb <- tbl_df( df )
tb <- tb %.%
group_by( yearID, teamID ) %.%
mutate( G_batting = max(G_batting) )
},
data.table = {
dt <- as.data.table( df )
dt[ , G_batting := max(G_batting), by = list( yearID, teamID ) ]
},
times = 500
)
qplot( data = mb, x …Run Code Online (Sandbox Code Playgroud) 我想使用ggplot2绘制三维数据在单纯形图上的投影.我以为我可以使用笛卡尔坐标来管理变换coord_trans(),但不知道如何准确地完成它.
这是我试过的:
simplex.y <- function( x1, x2, x3 ) {
return( sqrt(0.75) * x3 / (x1+x2+x3) )
}
simplex.x <- function( x1, x2, x3 ) {
return( (x2 + 0.5 * x3) / (x1+x2+x3) )
}
x <- data.frame(
x1 = c( 0, 0, 1, 0.1, 0.6, 0.2 ),
x2 = c( 0, 1, 0, 0.3, 0.2, 0.8 ),
x3 = c( 1, 0, 0, 0.6, 0.2, 0.0 )
)
require(ggplot2)
ggplot( data = x, aes( x = …Run Code Online (Sandbox Code Playgroud) 假设我有一个像这样的data.frame:
df <- matrix( rnorm(100), nrow = 10)
rownames(df) <- LETTERS[1:10]
molten <- melt(df)
molten$na <- FALSE
molten[ round(runif(10, 0, 100 )), "na" ] <- T
head(molten)
Var1 Var2 value na
1 A 1 -0.2413015 FALSE
2 B 1 1.5077282 FALSE
3 C 1 -1.0798806 TRUE
4 D 1 2.0723791 FALSE
Run Code Online (Sandbox Code Playgroud)
现在,我想使用ggplot绘制一个tile(或栅格)图并标记那些具有的tile na=TRUE.目前我将标记绘制为点:
g <- ggplot( molten ) +
geom_raster( aes( x = Var1, y = Var2, fill = value ) ) +
scale_fill_gradient2( low = "blue", …Run Code Online (Sandbox Code Playgroud) 我正在使用git作为RStudio中的版本控制系统.我有几个不同版本的R脚本保存在git中.可以说,我删除了一段代码,但我现在决定将代码重新插入到当前的R脚本中.我知道代码包含该功能ddply.这是我目前的工作流程:
我在RStudio中打开终端,然后输入:
git grep ddply $(git rev-list --all)
Run Code Online (Sandbox Code Playgroud)
这会带来数百行代码,例如:
2c975e1faee880545546646648f5be2b55f60fc757c29828f1f:reports/:data <- ddply(x, .(as.factor(y), summarise, mean(count))
2c975e1faee880545546646648f5be2b55f60fc757c29828f1f:reports/:data <- ddply(x, .(as.factor(y), summarise, mean(count))
2c975e1faee880545546646648f5be2b55f60fc757c29828f1f:reports/:data <- ddply(x, .(as.factor(y), summarise, mean(count))
2c975e1faee880545546646648f5be2b55f60fc757c29828f1f:reports/:data <- ddply(x, .(as.factor(y), summarise, mean(count))
2c975e1faee880545546646648f5be2b55f60fc757c29828f1f:reports/:data <- ddply(x, .(as.factor(y), summarise, mean(count))
Run Code Online (Sandbox Code Playgroud)
我筛选代码并最终找到我正在寻找的位.我从终端复制相关的代码位并将其粘贴回我的R脚本.在代码可用之前,我需要删除这个位:2c975e1faee880545546646648f5be2b55f60fc757c29828f1f:reports/:
目前,这感觉就像是一种相当缓慢而笨重的方式来重新使用旧的版本控制代码,而且我可能最好使用Rhistory.
是否有更快的方法将版本控制的代码从git中获取并返回到RStudio中的R脚本?
我尝试实现与dlply相同的功能data.table.所以就像一个非常简单的例子:
library(plyr)
library(data.table)
dt <- data.table( p = c("A", "B"), q = 1:2 )
dlply( dt, "p", identity )
$A
p q
1 A 1
$B
p q
1 B 2
dt[ , identity(.SD), by = p ]
p q
1: A 1
2: B 2
foo <- function(x) as.list(x)
dt[ , foo(.SD), by = p ]
p q
1: A 1
2: B 2
Run Code Online (Sandbox Code Playgroud)
显然,返回值foo折叠为一data.table.而且我不希望使用dlply,因为它通过拆分data.tables为data.frames以foo使内进一步data.table运营 …
希望这不是一个太愚蠢的问题,但仍然是一个R初学者我有一个严重的问题与tapply.让我们说
factors <- as.factor( c("a", "b", "c", "a", "b", "c", "a", "b", "c") )
values <- c( 1, 2, 3, 4, 5, NA, 7, NA, NA )
tapply(
values,
factors,
function(x){
if( sum(is.na(x)) == 1 ){
x[ is.na(x) ] <- 0
}
return(x)
}
)
Run Code Online (Sandbox Code Playgroud)
结果是
$a
[1] 1 4 7
$b
[1] 2 5 0
$c
[1] 3 NA NA
Run Code Online (Sandbox Code Playgroud)
但是,我需要的是获得一个保留原始值顺序的向量,即:
c( 1,2,3,4,5,NA,7,0,NA )
Run Code Online (Sandbox Code Playgroud)
提前谢谢了.
我经常遇到使用d*ply或时需要访问实际id变量的问题l*ply.一个简单(但无意义)的例子是:
df1 <- data.frame( p = c("a", "a", "b", "b"), q = 1:4 )
df2 <- data.frame( m = c("a", "b" ), n = 1:2 )
d_ply( df1, "p", function(x){
actualId <- unique( x$p )
print( mean(x$q)^df2[ df2$m == actualId, "n" ] )
})
Run Code Online (Sandbox Code Playgroud)
所以在d*ply功能方面我可以帮助自己unique( x$p ).但是当谈到时l*ply,我不知道如何访问相应列表元素的名称.
l_ply( list(a = 1, b = 2, c = 3), function(x){
print( <missing code> )
})
# desired output
[1] "a"
[1] "b" …Run Code Online (Sandbox Code Playgroud) 我正在尝试ncdf在Ubuntu 10.04服务器上安装r包:
install.packages("ncdf", type = "source")
Installing package into ‘/home/me/R/x86_64-pc-linux-gnu-library/3.0’
(as ‘lib’ is unspecified)
trying URL 'http://cran.rstudio.com/src/contrib/ncdf_1.6.6.tar.gz'
Content type 'application/x-gzip' length 79403 bytes (77 Kb)
opened URL
==================================================
downloaded 77 Kb
* installing *source* package ‘ncdf’ ...
** package ‘ncdf’ successfully unpacked and MD5 sums checked
checking for nc-config... no
checking for gcc... gcc -std=gnu99
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether …Run Code Online (Sandbox Code Playgroud) 我编写了一个小型Java客户端,它在Rserver上进行了一些计算.为此,在实际计算完成之前,还有functions.r- 和libraries.r服务器端的文件,这些文件必须是来源的.
目前我在每个新连接上加载文件:
import org.rosuda.REngine.Rserve.RConnection;
public class RserveTester {
public void doOnRserve() {
RConnection c = new RConnection( "rserve.domain.local" );
c.login( "foo", "user" );
c.eval("source(\"/home/rserve/lib/libraries.r\")");
c.eval("source(\"/home/rserve/lib/functions.r\")");
c.eval( "someCalculation()" )
c.close();
}
}
Run Code Online (Sandbox Code Playgroud)
其中,doOnRserve()由于在一分钟内客户端几次,一些事件被调用.
我的问题是:是否有可能只采购一次图书馆,以便在没有个人采购的所有新RSession期间可以使用它们?
我试着在客户端做类似的事情:
c.serverSource("/home/rserve/lib/libraries.r" )
c.serverSource("/home/rserve/lib/functions.r" )
Run Code Online (Sandbox Code Playgroud)
这给了我以下异常(不明白为什么这eval不起作用)
org.rosuda.REngine.Rserve.RserveException: serverSource failed, request status: access denied (local to the server)
Run Code Online (Sandbox Code Playgroud)
我可以用特定的方式启动Rserve .Rprofile吗?
编辑:
基本上,接缝有三种可能的方法:
new RConnection()EDIT2:
Rserve版本v0.6-8(338)
R版本2.15.2 for x86_64-pc-linux-gnu.
我刚刚Rmpi使用本教程安装:Mac OS-X Mountain Lion上的http://www.stats.uwo.ca/faculty/yu/Rmpi/mac_os_x.htm.我只需要使用Rmpi来使用所有内核,而不是在硬件集群或类似设备上进行部署.
实际上,一切正常,但现在我体验到,每当我没有活跃的互联网连接(如坐在火车上或只是转动无线)产卵奴隶就会失败,我想知道这是否应该像这样工作?
> require( Rmpi )
> mpi.spawn.Rslaves( nslaves=2 )
--------------------------------------------------------------------------
At least one pair of MPI processes are unable to reach each other for
MPI communications. This means that no Open MPI device has indicated
that it can be used to communicate between these processes. This is
an error; Open MPI requires that all MPI processes be able to reach
each other. This error can sometimes be the result of forgetting to …Run Code Online (Sandbox Code Playgroud)