在mts对象上使用apply(或sapply)会在发送到函数时删除其时间序列属性.我应该如何在mts对象的每个时间序列上应用相同的函数(使用ts输入和ts输出)并返回它(最好是mts)[我的意思是除了使用for循环]?
例如,假设我编写了一个返回时间序列趋势的函数(使用stl)
myfunc <- function(x) {
return(stl(x,"per")$time.series[,2])
}
Run Code Online (Sandbox Code Playgroud)
现在有一个样本mts
z <- ts(matrix(rnorm(90), 30, 3), start=c(1961, 1), frequency=4)
class(z)
Run Code Online (Sandbox Code Playgroud)
只发送其中一个时间序列是正确的:
myfunc(z[,1]) # works correctly, returns the trend of first series
Run Code Online (Sandbox Code Playgroud)
我的功能不适用于多个时间序列,因此:
myfunc(z) # will not work returning the error below
Error in stl(x, "per") : only univariate series are allowed
Run Code Online (Sandbox Code Playgroud)
在mts对象上使用apply将每个时间序列作为向量发送,而不是保留其时间序列属性(tsp):
apply(z,2,myfunc) # will not work returning the error below
Error in stl(x, "per") :
series is not periodic or has less than two periods
Run Code Online (Sandbox Code Playgroud) 我正在使用ggplot2准备一个情节,我想添加一个基于加权最小二乘估计的趋势线.
在基础图形中,这可以通过将WLS模型发送到abline:
mod0 <- lm(ds$dMNP~ds$MNP)
mod1 <- lm(ds$dMNP~ds$MNP, weights = ds$Asset)
symbols(ds$dMNP~ds$MNP, circles=ds$r, inches=0.35)
#abline(mod0)
abline(mod1)
Run Code Online (Sandbox Code Playgroud)
在GGPLOT2我设置的参数weight中geom_smooth,但没有改变:
ggplot(ds, aes(x=MNP, y=dMNP, size=Asset) +
geom_point(shape=21) +
geom_smooth(method = "lm", weight="Asset", color="black", show.legend = FALSE)
Run Code Online (Sandbox Code Playgroud)
这给了我同样的情节
ggplot(ds, aes(x=MNP, y=dMNP, size=Asset) +
geom_point(shape=21) +
geom_smooth(method = "lm", color="black", show.legend = FALSE)
Run Code Online (Sandbox Code Playgroud) 我想将data.frame的内容显示为slidify中的表.我知道如何使用ascii库从data.frames创建Markdown表,但是当我尝试使用slidify,而不是在输出html中看到表时,我看到了一堆关于ascii表的内部结构的信息.
那么如何在slidify中打印例如head(some.data.frame)?
编辑:
实际上我想在CRAN任务视图中显示一个视图表,现在我在Markdown中手动输入表格:
Views | Content
--------|--------
Bayesian| Bayesian Inference
ChemPhys| Chemometrics and Computational Physics
ClinicalTrials| Clinical Trial Design, Monitoring, and Analysis
Run Code Online (Sandbox Code Playgroud)
我想从ctv包中自动创建这个表.我已经在data.frame中收集了我需要的信息:
library(ctv)
list.of.views <- available.views()
X <- data.frame(View=NA,Description=NA)
for(i in 1:length(list.of.views))
{
X[i,1] <- list.of.views[[i]]$name
X[i,2] <- list.of.views[[i]]$topic
}
head(X)
Run Code Online (Sandbox Code Playgroud)
结果
View Description
1 Bayesian Bayesian Inference
2 ChemPhys Chemometrics and Computational Physics
3 ClinicalTrials Clinical Trial Design, Monitoring, and Analysis
4 Cluster Cluster Analysis & Finite Mixture Models
5 DifferentialEquations Differential Equations
6 Distributions …Run Code Online (Sandbox Code Playgroud) 我使用rworldmapWorldBank Data 的包,我喜欢它.我想为伊朗绘制地图,并提供与每个省相关的数据.这样做的步骤是什么?我知道我们可以R为像美国这样的国家绘制这样的地图,但不是所有国家都可以.
这个问题很简单,据我搜索,其他语言中也有类似的问题,但 R 中没有。
我想用 R 代码下载一个文件,但在下载之前,我想打印出下载时间的大小和估计。有没有办法直接在 R 基础上或使用curl 实用程序来执行此操作?