基本上,当setMethod
or或(setGeneric
)中存在许多参数时,它的工作速度非常慢.
这是一个基本的例子:
setClassUnion(name = "mNumeric", members = c("missing", "numeric"))
setClass(Class = "classA", representation = representation(ID = "character"))
setGeneric("foo", function(r, i, ..., m = 1, D = 1, U = 999, K = 0.005,
E1 = -5, E2 = 5, E3 = 1, E4 = 1, E5 = 1, E6 = 1,
A1 = -5, A2 = 5, A3 = 1, A4 = 1, A5 = 1, A6 = 1)
{standardGeneric ("foo")})
setMethod(f = "foo",
signature = …
Run Code Online (Sandbox Code Playgroud) 目前我正在使用相对较大的数据文件,而我的计算机不是超级计算机.我正在临时创建这些数据集的许多子集,而不是从工作区中删除它们.显然,这些正在使许多变量混乱.但是,在R的性能上有许多未使用的变量会有什么影响吗?(即计算机的内存是否在某些时候填满?)
编写代码时,我应该养成去除未使用变量的习惯吗?值得吗?
x <- rnorm(1e8)
y <- mean(x)
# After this point I will not use x anymore, but I will use y
# Should I add following line to my code? or
# Maybe there will not be any performance lag if I skip the following line:
rm(x)
Run Code Online (Sandbox Code Playgroud)
我不想在我的代码中添加另一行.而不是我的代码看起来混乱,我更喜欢我的工作区杂乱(如果没有性能改善).
我想发布一个这样的基本博客文章:( 示例博客文章)使用我的R markdown文件.
但我想在Wordpress中发布它(而不是wordpress.com).
这些解决方案都不适合我.我放弃.复制和粘贴代码和格式是非常低效的.我无法正确对齐我的输出.
有没有其他方法我没有偶然发现并可能解决(或缓解)我的问题?
在此先感谢任何回复..
在一个函数中,我想计算数值,给它们命名并返回一个NumericVector
在 Rcpp 中排序的值。我可以对向量进行排序(使用this),但值名称的顺序保持不变。
library(Rcpp)
x <- c(a = 1, b = 5, c = 3)
cppFunction('
NumericVector foo(NumericVector x) {
std::sort(x.begin(), x.end());
return(x);
}')
foo(x)
## a b c
## 1 3 5
Run Code Online (Sandbox Code Playgroud)
我希望函数返回这个:
## a c b
## 1 3 5
Run Code Online (Sandbox Code Playgroud)
是否可以?我怎样才能做到这一点?
我想在直方图中添加密度线(实际上是正常密度).
假设我有以下数据.我可以通过ggplot2
以下方式绘制直方图:
set.seed(123)
df <- data.frame(x = rbeta(10000, shape1 = 2, shape2 = 4))
ggplot(df, aes(x = x)) + geom_histogram(colour = "black", fill = "white",
binwidth = 0.01)
Run Code Online (Sandbox Code Playgroud)
我可以使用以下方法添加密度线:
ggplot(df, aes(x = x)) +
geom_histogram(aes(y = ..density..),colour = "black", fill = "white",
binwidth = 0.01) +
stat_function(fun = dnorm, args = list(mean = mean(df$x), sd = sd(df$x)))
Run Code Online (Sandbox Code Playgroud)
但这不是我真正想要的,我希望这个密度线适合计数数据.
我发现了一个类似的帖子(HERE)提供了解决这个问题的方法.但它在我的情况下不起作用.我需要一个任意的扩展因子来得到我想要的东西.这根本不是一般化的:
ef <- 100 # Expansion factor
ggplot(df, aes(x = x)) +
geom_histogram(colour = "black", fill = "white", …
Run Code Online (Sandbox Code Playgroud) 我想找到给定区间内标准正态分布的均值。
例如,如果我将标准正态分布一分为二 ([-Inf:0] [0:Inf]),我想得到每一半的平均值。
以下代码几乎完全符合我的要求:
divide <- 2
boundaries <- qnorm(seq(0,1,length.out=divide+1))
t <- sort(rnorm(100000))
means.1 <- rep(NA,divide)
for (i in 1:divide) {
means.1[i] <- mean(t[(t>boundaries[i])&(t<boundaries[i+1])])
}
Run Code Online (Sandbox Code Playgroud)
但是我需要一种更精确(和优雅)的方法来计算这些数字(means.1)。
我尝试了以下代码但它不起作用(可能是因为我缺乏概率知识)。
divide <- 2
boundaries <- qnorm(seq(0,1,length.out=divide+1))
means.2 <- rep(NA,divide)
f <- function(x) {x*dnorm(x)}
for (i in 1:divide) {
means.2[i] <- integrate(f,lower=boundaries[i],upper=boundaries[i+1])$value
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?提前致谢。
假设我有以下数据集:
set.seed(seed=10)
n <- 10000
s.data <- data.frame(score = rnorm(n,500,100),
gender = sample(c("Male","Female"),size=n,replace=T,prob=c(.4,.6)),
major = sample(c("A","B","C","D"),size=n,replace=T,prob=c(.02,.25,.05,.68)))
Run Code Online (Sandbox Code Playgroud)
我创建以下直方图:
require(ggplot2)
ggplot(s.data, aes(x=score)) + facet_wrap(~ major) +
geom_histogram(binwidth=50,colour="black", fill="white")
Run Code Online (Sandbox Code Playgroud)
由于我想了解有关主要A和C的更多细节,我绘制了一个密度直方图:
ggplot(s.data, aes(x=score)) + facet_wrap(~ major) +
geom_histogram(binwidth=50,aes(y = ..density..),colour="black", fill="white")
Run Code Online (Sandbox Code Playgroud)
完美到现在为止.
当我尝试使用分类变量(而不是连续变量)做同样的事情时,我可以做频率但无法绘制密度:
ggplot(s.data, aes(gender)) +
geom_histogram(colour="black", fill="white") +
facet_wrap(~ major)
Run Code Online (Sandbox Code Playgroud)
我想要的.
但是我不知道这张图:
ggplot(s.data, aes(gender)) +
geom_histogram(aes(y = ..density..),colour="black", fill="white") +
facet_wrap(~ major)
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?提前致谢.
假设我有一个学生在考试中得分的数据.
set.seed(1)
df <- data.frame(question = 0:10,
resp = c(NA,sample(c("Correct","Incorrect"),10,replace=TRUE)),
score.after.resp=50)
for (i in 1:10) {
ifelse(df$resp[i+1] == "Correct",
df$score.after.resp[i+1] <- df$score.after.resp[i] + 5,
df$score.after.resp[i+1] <- df$score.after.resp[i] - 5)
}
df
Run Code Online (Sandbox Code Playgroud)
.
question resp score.after.resp
1 0 <NA> 50
2 1 Correct 55
3 2 Correct 60
4 3 Incorrect 55
5 4 Incorrect 50
6 5 Correct 55
7 6 Incorrect 50
8 7 Incorrect 45
9 8 Incorrect 40
10 9 Incorrect 35
11 10 Correct 40
Run Code Online (Sandbox Code Playgroud)
我想获得以下图表: …
我想在ggplot中将希腊字母添加到文本中.这是我想要做的:
library(ggplot2)
df <- data.frame(x = rnorm(10), y = rnorm(10))
xIntercept <- mean(df$x)
yIntercept <- mean(df$y)
temp <- paste("theta = ", xIntercept) # This Works
ggplot(df, aes(x = x, y = y)) + geom_point() +
annotate("text", x = xIntercept, y = yIntercept, label = temp, color = "blue")
Run Code Online (Sandbox Code Playgroud)
而不是"theta",我想使用希腊字母theta.我试过这个链接但不能这样做.我尝试了以下代码但没有发生任何事
temp <- list(bquote(theta == .(xIntercept)))
temp <- bquote(theta == .(xIntercept))
temp <- expression(theta, "=", xIntercept)
temp <- c(expression(theta), paste0("=", xIntercept))
Run Code Online (Sandbox Code Playgroud) 我正在尝试为S4类编写子集方法.this S4 class is not subsettable
无论我尝试什么,我都会收到错
这是一个最小的例子:
setClass(Class = "A", representation = representation(ID = "character"))
setClass(Class = "B", representation = representation(IDnos = "list"))
a1 <- new(Class = "A", ID = "id1")
a2 <- new(Class = "A", ID = "id2")
B1 <- new(Class = "B", IDnos = c(a1, a2))
Run Code Online (Sandbox Code Playgroud)
当我输入:
B1@IDnos[[1]]
Run Code Online (Sandbox Code Playgroud)
我得到了我想要的东西:
Run Code Online (Sandbox Code Playgroud)> An object of class "A" > Slot "ID": > [1] "id1"
但我想通过写一些类似于:B1[1]
或者如果不是这样来得到这个B1[[1]]
从这篇文章中,我有了一些想法,并试图模仿作者写的内容.但它在我的情况下不起作用:
setMethod("[", c("B", "integer", "missing", "ANY"),
function(x, i, j, …
Run Code Online (Sandbox Code Playgroud)