由于我经常遇到需要S4编程来保持概述的情况,我收集了很多关于S4对象,方法和编程的资料.我在这里列出了它们作为参考.请添加您自己的来源.
在网上
methods帮助文件:从包装方法中,很多必要的信息都可以找到帮助文件图书
PS:如果有人找到社区复选框,您可以将其设为社区拥有的帖子.不知何故,我再也无法在编辑窗口中找到它......
我正在尝试编写一个删除对象的函数(如果它存在).原因是我想摆脱日志消息错误:找不到对象'arg'.我尝试了以下方法:
ifrm <- function(arg)
{
if(exists(as.character(substitute(arg)))){rm(arg)}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,如果对象存在,则不会删除该对象
> ifrm <- function(arg)
+ {
+ if(exists(as.character(substitute(arg)))){rm(arg)}
+ }
> a <- 2
> ifrm(a)
> a
[1] 2
Run Code Online (Sandbox Code Playgroud)
我在这里做错了什么提示?
最好的阿尔布雷希特
我有一个相同排序的数据帧列表.更具体的是这些是在使用AmeliaII包进行多次插补后得到的估算数据帧.现在我想创建一个结构相同的新数据帧,但包含在数据帧中计算的单元格的平均值.
我目前实现这一目标的方式如下:
## do the Amelia run ------------------------------------------------------------
a.out <- amelia(merged, m=5, ts="Year", cs ="GEO",polytime=1)
## Calculate the output statistics ----------------------------------------------
left.side <- a.out$imputations[[1]][,1:2]
a.out.ncol <- ncol(a.out$imputations[[1]])
a <- a.out$imputations[[1]][,3:a.out.ncol]
b <- a.out$imputations[[2]][,3:a.out.ncol]
c <- a.out$imputations[[3]][,3:a.out.ncol]
d <- a.out$imputations[[4]][,3:a.out.ncol]
e <- a.out$imputations[[5]][,3:a.out.ncol]
# Calculate the Mean of the matrices
mean.right <- apply(abind(a,b,c,d,e,f,g,h,i,j,along=3),c(1,2),mean)
# recombine factors with values
mean <- cbind(left.side,mean.right)
Run Code Online (Sandbox Code Playgroud)
我想通过使用apply,plyr等有更好的方法来做到这一点,但作为一个R新手,我真的有点迷失在这里.你对此有什么建议吗?
我正在ggplot使用该grid.arrange功能准备一个37 秒的网格.为了节省轴标签当前占用的空间并添加一些信息,Sys.time()我会在图形网格的右下角添加一个框.
使用mtcars数据的最小示例可以在下面找到.真实数据将涵盖x轴上的非常不同的范围,以及刻面不是一个选项.
有没有办法添加"文本框",如下面的*.pdf所示,以使用例如cat或print?添加更多信息?任何提示都将受到高度赞赏.
# load needed libraries
library(ggplot2)
library(gridExtra)
# Set loop counter and create list to store objects
imax=37
plist <- list()
# loop to generate 37 ggplot objects
# the real example covers different ranges on x-axis so facetting
# is not an option
for(i in 1:imax){
p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_line() + ggtitle(i)
plist[[i]] <- p
}
# …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用欧洲统计局提供的shapefile和数据生成Choroplete地图.shapefile已在此处下载:使用此帖子中的 JD Longs代码.
这是重现下面发布的图的最小代码.
library(maptools)
tmpdir <- tempdir()
url <- 'http://ec.europa.eu/eurostat/cache/GISCO/geodatafiles/NUTS_2010_03M_SH.zip'
file <- basename(url)
download.file(url, file)
unzip(file, exdir = tmpdir )
shapeFile <- paste(tmpdir,"/Shape/data/NUTS_RG_03M_2010", sep="")
EU <- readShapeSpatial(shapeFile)
plot(EU)
Run Code Online (Sandbox Code Playgroud)
我的问题是,我希望情节区域只关注欧洲,但由于海外地区(法国和西班牙),情节没有正确的焦点.在上面的例子中,有一种简单的方法可以"裁剪"绘图区域吗?
我想要摆脱的多边形是"Country_Shape"的一部分,所以过滤它们是没有选择的.我试图通过在plot命令中定义xlim和ylim参数来实现我的目标,但没有成功.我使用locator()从图形设备获取坐标,但是插入值并没有提供想要的结果.
