我正在尝试强化我用rgdal加载到R中的形状文件,但是我收到以下错误:
"Error: TopologyException: found non-noded intersection between LINESTRING
(34.7279 1.59723, 34.7278 1.59729) and LINESTRING
(34.7278 1.59723, 34.7278 1.59729) at 34.727793021883102 1.5972887049072426"
Run Code Online (Sandbox Code Playgroud)
我从maplibrary.org使用非洲大陆的形状文件.它可以在我的保管箱中找到:https: //www.dropbox.com/s/etqdw3nky52czv4/Africa%20map.zip
我使用以下代码:
library(rgdal)
library(ggplot2)
africa = readOGR("Africa_SHP", layer = "Africa")
africa.map = fortify(africa, region="COUNTRY")
Run Code Online (Sandbox Code Playgroud)
我得到了之前提到的错误.我认为R在多边形方面存在一些问题 - 有没有办法解决这个问题?
我rgl为我的数据的每个因子级别生成了包含3D图,并将它们保存为png.我的数据有30个不同的级别,导致30个不同的图像文件.现在我想将这些png组合成一个单独的图.
我会像这样显示它们:

以下示例说明了我想要做的事情:
library(rgl)
library(png)
library(gridExtra)
library(ggplot2)
## creates a png in the working directory which can be used as an example
example(surface3d)
rgl.snapshot("example.png")
rgl.close()
## imports the png files; in the example, the same file is imported multiple times.
if(exists("png.df")) rm(png.df)
for (i in 1:9) {
png.i <- readPNG("example.png")
g <- rasterGrob(png.i, interpolate=TRUE)
g <- g$raster
g <- as.vector(g)
g <- matrix(g, nrow = 256, ncol = 256, dimnames = list(1:256, 1:256))
df.i <- data.frame(i = rep(row.names(g), …Run Code Online (Sandbox Code Playgroud) 我现在有两种方式的班级建设:
首先,
setMethod("initialize", signature(.Object = "BondCashFlows"),
function(.Object, x, y, ...){
do some things .Object@foo = array[,m]
}
Run Code Online (Sandbox Code Playgroud)
第二,
BondCashFlows <- function(){do some things new("BondCashFlows", ...)
Run Code Online (Sandbox Code Playgroud)
所以,我的问题是为什么我甚至不得不打扰第一个,因为第二个更多的是用户友好的方式创建对象BondCashFlows?
我明白第一个是课上的方法,但我不知道为什么要这样做
我刚刚安装rsqlserver好了(没有错误)
install_github('rsqlserver', 'agstudy',args = '--no-multiarch')
Run Code Online (Sandbox Code Playgroud)
并创建了与我的数据库的连接:
> library(rClr)
> library(rsqlserver)
Warning message:
multiple methods tables found for ‘dbCallProc’
> drv <- dbDriver("SqlServer")
> conn <- dbConnect(drv, url = "Server=MyServer;Database=MyDB;Trusted_Connection=True;")
>
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试使用数据时dbGetQuery,我收到此错误:
> df <- dbGetQuery(conn, "select top 100 * from public2013.dim_Date")
Error in clrCall(sqlDataHelper, "GetConnectionProperty", conn, prop) :
Type: System.MissingMethodException
Message: Method not found: 'System.Object System.Reflection.PropertyInfo.GetValue(System.Object)'.
Method: System.Object GetConnectionProperty(System.Data.SqlClient.SqlConnection, System.String)
Stack trace:
at rsqlserver.net.SqlDataHelper.GetConnectionProperty(SqlConnection _conn, String prop)
>
Run Code Online (Sandbox Code Playgroud)
当我尝试使用获取结果时dbSendQuery,我也会收到错误.
> res <- dbSendQuery(conn, "select …Run Code Online (Sandbox Code Playgroud) 我有一台64位的PC.在Rstudio v0.98.953中使用ggplot 2,当前R更新.
我是ggplot的新手,对R的熟练程度有限.我有以下代码:
# Simple Bar Plot for Multivariate OR by NVP quartile
dat <- data.frame(
QUAR = factor(c("1","2","3","4"), levels=c("1","2","3","4")),
aOR = c(1.00, 2.47, 3.33, 9.17),
lowerCI = c(1.00, 1.09, 1.33, 3.20),
upperCI = c(1.00, 5.60, 8.30, 26.0)
)
dat
library(ggplot2)
myplot = ggplot(data=dat, aes(x=QUAR, y=aOR, fill=QUAR)) +
geom_bar(colour="black", width=.8, stat="identity") +
scale_fill_manual(values=c("#e8e7e7", "#c3bfbf", "#908c8c", "#363434")) +
guides(fill=FALSE) +
xlab("XXX Quartile") + ylab("YYY") +
geom_text(data=dat, aes(x=QUAR, y=aOR, label=round(aOR, 1)), vjust=-0.5) +
coord_cartesian(ylim = c(0, 11)) +
ggtitle("Graph")
myplot
# …Run Code Online (Sandbox Code Playgroud) R heatmap() 文档说 forRowv和Colv(即行和列排序参数):
如果默认情况下缺少其中一个,则相应树状图的排序将按行/列的平均值进行,即,在行的情况下,Rowv <- rowMeans(x, na.rm = na.rm) 。
我认为这很简单,但现在我想默认排序算法中一定还有更多的东西。
让我们得到这个相关矩阵:
m = matrix(nrow=7, ncol = 7, c(1,0.578090870728824,0.504272263365781,0.526539138953634,0.523049273011785,0.503296777916728,0.638770769734758,0.578090870728824,1,0.59985543029105,0.663649941610205,0.630998114483389,0.66814547270115,0.596161809036262,0.504272263365781,0.59985543029105,1,0.62468477053142,0.632715952452297,0.599037620726669,0.607925540860012,0.526539138953634,0.663649941610205,0.62468477053142,1,0.7100707346884,0.738094117424525,0.639668277558577,0.523049273011785,0.630998114483389,0.632715952452297,0.7100707346884,1,0.651331659193182,0.64138213322125,0.503296777916728,0.66814547270115,0.599037620726669,0.738094117424525,0.651331659193182,1,0.612326706593738,0.638770769734758,0.596161809036262,0.607925540860012,0.639668277558577,0.64138213322125,0.612326706593738,1))
m
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] 1.0000000 0.5780909 0.5042723 0.5265391 0.5230493 0.5032968 0.6387708
[2,] 0.5780909 1.0000000 0.5998554 0.6636499 0.6309981 0.6681455 0.5961618
[3,] 0.5042723 0.5998554 1.0000000 0.6246848 0.6327160 0.5990376 0.6079255
[4,] 0.5265391 0.6636499 0.6246848 1.0000000 0.7100707 0.7380941 0.6396683
[5,] 0.5230493 0.6309981 0.6327160 0.7100707 1.0000000 0.6513317 0.6413821
[6,] 0.5032968 0.6681455 0.5990376 0.7380941 0.6513317 1.0000000 0.6123267 …Run Code Online (Sandbox Code Playgroud) 我试图使用该lag函数在xts对象中创建一个滞后向量.它在使用$符号(例如x.ts$r1_lag)在xts对象中定义新向量时起作用,但在使用方括号定义新变量时也是如此,即xts[,"r1_lag"].见下面的代码:
library(xts)
x <- data.frame(date=seq(as.Date('2015-01-01'), by='days', length=100),
runif(1e2), runif(1e2), runif(1e2))
colnames(x) <- c("date", "r1", "r2", "r3")
#the following command works
x.ts <- xts(x, order.by=x$date)
x.ts$r1_lag <- lag(x.ts$r1)
# but the following does not (says subscript is out of bounds)
x.ts <- xts(x, order.by=x$date)
x.ts[,"r1_lag"] <- lag(x.ts[,"r1"])
Run Code Online (Sandbox Code Playgroud)
我需要使用[]符号而不是$符号来引用向量,因为如果我想对多个xts对象中的向量运行滞后变换(多个xts对象列表中的向量),我无法在其中定义新向量使用$符号的对象,即我不能使用下面的程式化循环中的符号来定义新的向量:
for (i in letters) {
for (j in variables) {
macro.set.ts$i$paste(j,"_L1",sep="") <- lag(macro.set.ts[[i]][,j])
macro.set.ts$i$paste(j,"_L2",sep="") <- lag(macro.set.ts[[i]][,j], 2)
macro.set.ts$i$paste(j,"_L4",sep="") <- …Run Code Online (Sandbox Code Playgroud) 标题相当清楚,但我试图取两个矩阵A和B,并输出一个矩阵C,它具有两个矩阵中的最小元素.
cij = min(aij,bij)
这就是我考虑这样做的方式:
C <- matrix(ncol = ncol(A), nrow = nrow(A), 0)
for (i in 1:ncol(C)) {
Y <- rbind(A[i,], B[i,])
C[i,] <- apply(Y, 2, min)
}
Run Code Online (Sandbox Code Playgroud)
但是,我希望这可以被矢量化,但我想不出怎么做.我没有找到任何东西,如果有人有任何想法,我会非常感激.
谢谢!
有一个字符串向量列表:
xx <- c("concord wanderer basic set air snug beige",
"concord wanderer basic set air snug black noir",
"concord wanderer basic set air snug blue bleu",
"concord wanderer basic set air snug brown marron",
"concord wanderer basic set air snug green vert",
"concord wanderer basic set air snug grey gris",
"concord wanderer basic set air snug red rouge",
"concord wanderer basic set air snug rose" )
Run Code Online (Sandbox Code Playgroud)
我试图在向量的元素之间获得最小的共享部分,例如,在这里我应该得到:
"concord wanderer basic set air snug"
Run Code Online (Sandbox Code Playgroud)
xx是前一个过程的结果,所以我确信元素之间存在共享部分.但被删除的部分并不总是在他的结尾.
使用strsplit和`table我得到这个部分解决方案,但它有点棘手,我放松了原始的单词顺序:
table_x …Run Code Online (Sandbox Code Playgroud) 我使用tm_map(testfile,removeNumbers)来删除文本文件的编号.但是,我需要保留与ipv4和ipv6等词一起出现的数字.如何使用removeNumbers函数删除其他数字,但保留ipv4和ipv6附带的数字.
这是我使用的代码:
test.txt = "this is a test file with numbers 1,2 and 3.
The internet protocals ipv4 and ipv6"
library(tm)
test <- Corpus(DirSource('C:test'), readerControl = list(reader = readPlain))
test <- tm_map(test, removeNumbers)
inspect(test[1])
Run Code Online (Sandbox Code Playgroud)
输出:
$test.txt
this is a test file with numbers , and . The internet protocals ipv and ipv
Run Code Online (Sandbox Code Playgroud) r ×10
ggplot2 ×2
plot ×2
dendrogram ×1
fuzzy-search ×1
heatmap ×1
intersection ×1
nested-lists ×1
png ×1
rgdal ×1
rgl ×1
rsqlserver ×1
s4 ×1
sql-server ×1
string ×1
text-mining ×1
tm ×1
xts ×1