我写了一个创建条形图的函数.我想将这个图保存为pdf,并在应用此功能时将其显示在我的屏幕(x11)上.代码看起来像这样.
create.barplots <- function(vec)
{
x11() # opens the window
### Here is a code that creates a barplot and works perfectly
### but irrelevant for my question
dev.copy(pdf("barplots.table.2.pdf")) # is supposed to copy the plot in pdf
# under the name "barplots.table.2.pdf"
dev.off() # is supposed to close the pdf device
}
Run Code Online (Sandbox Code Playgroud)
这会产生以下错误:'device'应该是一个函数
当我将代码修改为:
create.barplots <- function(vec)
{
x11()
### Here is a code that creates a barplot and works perfectly
### but irrelevant for my question
dev.copy(pdf) # …Run Code Online (Sandbox Code Playgroud) 我想将我在函数中传递的字符串转换为对象(或列名).
我知道这有效:
df <- data.frame(A = 1:10, B = 11:20)
test.function <- function(x)
{
z <- df[[x]]
return(z)
}
test.function("A")
Run Code Online (Sandbox Code Playgroud)
我不想使用[[.]]运算符,因为有时它不实用甚至不适用.我在一个将字符串转换为"对象"的通用方法中得到了强调.因此我尝试了以下方法:
df <- data.frame(A = 1:10, B = 11:20)
test.function <- function(x)
{
z <- get(paste("df$", x, sep = ""))
return(z)
}
test.function("A")
Run Code Online (Sandbox Code Playgroud)
要么
df <- data.frame(A = 1:10, B = 11:20)
test.function <- function(x)
{
z <- as.name(paste("df$", x, sep = ""))
return(z)
}
test.function("A")
Run Code Online (Sandbox Code Playgroud)
要么
df <- data.frame(A = 1:10, B = 11:20)
test.function <- function(x)
{
z …Run Code Online (Sandbox Code Playgroud) 我有一个简单而奇怪的问题.
indices.list是一个列表,包含118,771个元素(整数或数字).通过应用函数unlist,我失去了大约500个元素.
看下面的代码:
> indices <- unlist(indices.list, use.names = FALSE)
>
> length(indices.list)
[1] 118771
> length(indices)
[1] 118248
Run Code Online (Sandbox Code Playgroud)
怎么可能?我检查了indices.list是否包含任何NA.但它没有:
> any(is.na(indices.list) == TRUE)
[1] FALSE
Run Code Online (Sandbox Code Playgroud)
data.set.merged是一个包含超过200,000行的数据帧.当我使用向量索引(显然长度为118,248)以获取data.set.merged的子集时,我得到一个118,771行的数据帧!那太奇怪了!
data.set.merged.2 <- data.set.merged[indices, ]
> nrow(data.set.2)
[1] 118771
Run Code Online (Sandbox Code Playgroud)
有什么想法在这里发生?
假设您有一个这样的数据框:
df <- data.frame(Nums = c(1,2,3,4,5,6,7,8,9,10), Cum.sums = NA)
> df
Nums Cum.sums
1 1 NA
2 2 NA
3 3 NA
4 4 NA
5 5 NA
6 6 NA
7 7 NA
8 8 NA
9 9 NA
10 10 NA
Run Code Online (Sandbox Code Playgroud)
你想要一个像这样的输出:
Nums Cum.sums
1 1 0
2 2 0
3 3 0
4 4 3
5 5 5
6 6 7
7 7 9
8 8 11
9 9 13
10 10 15
Run Code Online (Sandbox Code Playgroud)
Cum.sum列的4.元素是1和2的总和,Column Cum.sum的5.元素是2和3的总和,依此类推......这意味着,我想构建第一行的累积和,并将其保存在第二行.但是,我不希望正常的累积和,而是当前行上方的元素2行加上当前行上方3行的元素之和.
我已经尝试过使用sum和cumsum函数,但我失败了.
有任何想法吗? …
我想得到自举的t值和lm的自举p值.我有以下代码(基本上是从一篇论文中复制而来).
# First of all you need the following packages
install.packages("car")
install.packages("MASS")
install.packages("boot")
library("car")
library("MASS")
library("boot")
boot.function <- function(data, indices){
data <- data[indices,]
mod <- lm(prestige ~ income + education, data=data) # the liear model
# the first element of the following vector contains the t-value
# and the second element is the p-value
c(summary(mod)[["coefficients"]][2,3], summary(mod)[["coefficients"]][2,4])
}
Run Code Online (Sandbox Code Playgroud)
现在,我计算了bootstrapping模型,它给出了以下内容:
duncan.boot <- boot(Duncan, boot.function, 1999)
duncan.boot
ORDINARY NONPARAMETRIC BOOTSTRAP
Call:
boot(data = Duncan, statistic = boot.function, R = 1999)
Bootstrap Statistics : …Run Code Online (Sandbox Code Playgroud) 关于情节设置我有一个非常简单的问题.
我希望以特殊的方式在y轴上有刻度线(以及这些刻度线上的标签).例如,从3到9,一个单元.
这是代码:
windows()
par(yaxp = c(3, 9, 7))
plot(1:10)
Run Code Online (Sandbox Code Playgroud)
但它不起作用.我真的不明白为什么?我也尝试使用par()中的参数,如tck,tcl,yaxs,yaxt,yaxp和函数axis().其中以下代码为例:
windows()
par(yaxt = "n", yaxp = c(3, 9, 7))
plot(1:10)
Run Code Online (Sandbox Code Playgroud)
要么
windows()
par(yaxt = "n")
plot(1:10)
axis(2, at = c(3, 4, 5))
Run Code Online (Sandbox Code Playgroud)
不幸的是,我在每一个案例都失败了......任何想法?