我经常要将许多数字(数百)导出到一个文件中.目前我用
print('-dpsc2', outfile, '-append');
Run Code Online (Sandbox Code Playgroud)
我的代码使用了一个隐藏的数字,正在为每个新数字重用它.
现在,问题是我可以达到每秒8个数字的最大输出速度.在R中,您可以轻松地绘制每秒200个数字.有没有人有任何建议如何(大幅)加快MATLAB的出口能力?
一月
我在大学教数学和编程(与R),我是一个良好和一致的符号的忠实粉丝.请看一下R中的以下简单向量运算:
> v1 <- c(1,2,3)
> v2 <- c(4,5,6)
> v1 %*% v2
[,1]
[1,] 32
> t(v1) %*% v2
[,1]
[1,] 32
> v1 %*% t(v2)
[,1] [,2] [,3]
[1,] 4 5 6
[2,] 8 10 12
[3,] 12 15 18
> t(v1) %*% t(v2)
Error in t(v1) %*% t(v2) : non-conformable arguments
> v1 + v2
[1] 5 7 9
> v1 + t(v2)
[,1] [,2] [,3]
[1,] 5 7 9
> t(v1) + t(v2)
[,1] [,2] [,3] …Run Code Online (Sandbox Code Playgroud) 所以我知道在堆栈溢出之前已经问过并回答了这个问题,但答案还没有解决我的问题,所以我希望这个问题有新的发现,有人可以帮助我.
我在Ubuntu 12.04 lts和R版本3.2.2(2015-08-14)上运行RStudio.当我尝试使用绘图功能时,我得到以下错误信息:
> plot(cars)
Error in RStudioGD() :
Shadow graphics device error: r error 4 (R code execution error)
In addition: There were 50 or more warnings (use warnings() to see the first 50)
Run Code Online (Sandbox Code Playgroud)
警告()给了我:
Warning messages:
1: In grDevices:::png("/tmp/RtmpOdYXyC/1ec8e69b4b2b4902818e02cd828dbf5e.png", ... :
failed to load cairo DLL
2: In grDevices:::png("/tmp/RtmpOdYXyC/a4885ae9c7b846698a638ca9c65e2632.png", ... :
failed to load cairo DLL
3: In grDevices:::png("/tmp/RtmpOdYXyC/91faf2840f694bef8d04409cdc64e5c9.png", ... :
failed to load cairo DLL
4: In grDevices:::png("/tmp/RtmpOdYXyC/4f77d036b3bc4e9f815d1aa8c8d834da.png", ... :
failed to load cairo DLL
5: …Run Code Online (Sandbox Code Playgroud) ###Load libraries
library(ggplot2)
library(gtable)
###Build plot
d <- ggplot(mtcars, aes(x=gear)) +
geom_bar(aes(y=gear), stat="identity", position="dodge") +
facet_wrap(~cyl)
###Change height of strip text
g <- ggplotGrob(d)
g$heights[[3]] = unit(2,"in")
grid.newpage()
grid.draw(g)
Run Code Online (Sandbox Code Playgroud)
ggplot2_2.0.0)ggplot2_1.0.1)中土世界到底发生了什么?
我有一个包含超过 400.000 个观察值的数据框,我正在尝试向其中添加一列,其值取决于另一列,有时取决于多个列。
这是我正在尝试做的一个更简单的例子:
# Creating a data frame
M <- data.frame(c("A","B","C"),c(5,100,60))
names(M) <- c("Letter","Number")
#adding a column
M$Size <- NA
# if Number <= 50 Size is small,
# if Number is between 50 and 70, Size is Medium
# if Number is Bigger than 70, Size is Big
ifelse (M$Number <=50, M$Size <-"Small",
ifelse(M$Number <= 70,
M$Size <- "Medium",
M$Size <- "Big"
))
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,我得到的输出是:
[1] "Small" "Big" "Medium"
Run Code Online (Sandbox Code Playgroud)
但是 M 中的“大小”列始终是 ifelse 函数中的最后一个条件:
> print (M)
Letter Number …Run Code Online (Sandbox Code Playgroud) 是否可以直接将整数SEXP参数转换为整数而无需先将其转换为整数向量?
例:
#include <Rcpp.h>
SEXP f(SEXP n)
{
Rcpp::IntegerVector n_vec(n);
int n1 = n_vec[0];
...
return R_NilValue;
}
Run Code Online (Sandbox Code Playgroud) 我有一个数据框,其中变量是字符串.如何仅提取至少一个值与特定字符串匹配的列?例如,在下面的数据框中,我想要字符串"AB"的匹配,即我想要将包含列V1,V2和V5的另一个数据帧子集化.
V1 V2 V3 V4 V5
ABCD ABEF EFGJ AFASD JLKJLXKJ
LKJAF ROGIJ GREJWI SDFS ABKLJKJX
AFSD JLASDF JKLJ OIJPOI AFSD
Run Code Online (Sandbox Code Playgroud) 这似乎是一个基本问题,但我似乎无法在stackoverflow上找到答案.
如何获得以下效果:
f <- function(x = 1){x^2}
miracle(f)
[1] "x^2"
Run Code Online (Sandbox Code Playgroud)
上下文是一个闪亮的应用程序(由RStudio打包),其中我有一个textInput()函数,我提供了一个初始值x^2.虽然这有效:
textInput(inputId = "inFun", label = h4("Enter a function:"), value = "x^2")
Run Code Online (Sandbox Code Playgroud)
这不是:
textInput(inputId = "inFun", label = h4("Enter a function:"), value = f)
Run Code Online (Sandbox Code Playgroud)
似乎我需要在价值的rhs上使用类似"x ^ 2"的东西.
以下是我尝试过的几种变体的代表性示例:
eval(parse(text = f))
Error in as.character(x) :
cannot coerce type 'closure' to vector of type 'character'
f(x = "x")
Error in x^2 : non-numeric argument to binary operator
`f`
function(x){x^2}
f(x = `x`)
Error in f(x = x) …Run Code Online (Sandbox Code Playgroud) 我有以下代码块:
temp <- "44C"
sub("^([-+]?[0-9]+)([CF])$","\\2",temp)
Run Code Online (Sandbox Code Playgroud)
这正确返回Ç.
然而,当我尝试
temp <- "44"
sub("^([-+]?[0-9]+)([CF])$","\\2",temp)
Run Code Online (Sandbox Code Playgroud)
我期待一个空的矢量.相反,我得到" 44 ".
我说错了吗?
我正在使用该R软件包bnlearn来估计贝叶斯网络结构.它使用parallel包内置并行化.但是,这不起作用.
使用联机帮助页中的示例bnlearn::parallel integration:
library(parallel)
library(bnlearn)
cl = makeCluster(2)
# check it works.
clusterEvalQ(cl, runif(10)) # -> this works
data(learning.test)
res = gs(learning.test, cluster = cl)
Run Code Online (Sandbox Code Playgroud)
我在这里得到错误 "Error in check.cluster(cluster) : cluster is not a valid cluster object."
有人知道怎么做这个吗?