我正在显示时间序列数据,ggplot2但是刻度标签显示了一些奇怪的行为.可能我做错了但我在互联网上找不到任何帮助.这是一个例子:
#just sample data
time <- as.Date(seq(as.Date("2004/1/1"), as.Date("2009/12/1"), by = "1 month"))
data <- rnorm(nrow(test))+c(1:nrow(test))
test <- data.frame(time, data)
Run Code Online (Sandbox Code Playgroud)
我用:
q1 <- ggplot(data=test) + geom_line(aes(x=time, y=data))
q1 <- q1 + scale_x_date(major="years", minor="3 months", format="%Y-%m", lim=c(as.Date("2004/1/1"),as.Date("2009/12/1")), name="")
q1
Run Code Online (Sandbox Code Playgroud)
这会产生以下图表:

但根据我的理解,电网应该在2009/12/1结束 - 对吧?非常感谢你的帮助!
我尝试通过stat_function()in 叠加一个函数,ggplot但无法弄清楚我的错误.这个例子产生了一个漂亮的情节:
data <- data.frame(x=rt(10000, df=7))
ggplot(data=data, aes(x=x)) + geom_histogram(aes(y = ..density..)) +
stat_function(fun =dnorm, size=1, color='gray', args=list()) +
opts(title="Histogram of interest rate changes") + theme_bw()
Run Code Online (Sandbox Code Playgroud)

但是当我尝试叠加对数正态密度时,这不能按预期工作(或者我应该按预期说这不起作用;):
data <- data.frame(x=rf(10000, df1=7, df2=120))
ggplot(data=data, aes(x=x)) + geom_histogram(aes(y = ..density..)) +
stat_function(fun =dnorm, size=1, color='gray', args=list(log=TRUE)) +
opts(title="Histogram of interest rate changes") + theme_bw()
Run Code Online (Sandbox Code Playgroud)

所以这是我希望简单的问题:我在这里做错了什么?我想这是一个非常简单的问题我只是没有看到答案 - 对不起.
这一定是一个非常微不足道的问题,但我很难找到解决方案.这是我的问题:
这很有效
#I run a simple regression
data(mtcars)
dataf <- mtcars
summary(fit1 <- lm(mpg ~ wt, data=dataf))
#Then I merge the fitted values with the data frame
dataf$fit <- fitted(fit1)
Run Code Online (Sandbox Code Playgroud)
这(当然)不起作用
dataf[2,]<-NA
summary(fit2 <- lm(mpg ~ wt, data=dataf))
#of course the NA value reduces my lm output
dataf$fit2 <- fitted(fit2)
Error in `$<-.data.frame`(`*tmp*`, "fit2", value = c(23.3189679389035, :
replacement has 31 rows, data has 32
Run Code Online (Sandbox Code Playgroud)
但是我如何才能使第二个例子起作用呢?我通过row.namesin 尝试了一个解决方案model.matrix()但是当我在回归中包含某些因素时这不起作用(如果我理解这一点,这已被报告为错误).谢谢你的热心帮助!
有没有办法通过nominatim从请求中获得对象的道路类型?
例如
确实为我提供了大量的信息.然而,在地址居住的道路上没有信息.想获得类似的东西:乡村道路甚至只是等级*N*(其中N代表等级的水平.)我希望我正在寻找的东西是可以理解的:)提前谢谢!
从这个问题中,我得到了一个很棒的函数areaPolygon(),它给了我一个多边形坐标内的面积。但是,当我尝试使用该函数时,计算结果似乎很奇怪:
我首先创建两个点
require(fields)
coords <- c(11.3697193956209, 47.233380520521, 11.3723606043791,
47.235179479479)
coords <- matrix(coords, nrow=2, ncol=2, byrow=TRUE)
Run Code Online (Sandbox Code Playgroud)
然后检查这两者之间的距离:
rdist.earth(coords,coords,miles=FALSE)[1,2]
Run Code Online (Sandbox Code Playgroud)
获得:0.2827821公里(将是矩形的对角线)
我继续创建一个矩形
polygon <- matrix(coords, nrow=2, ncol=2)
polygon <- rbind(polygon, polygon)
polygon[4,2] <- polygon[1,2]
polygon[4,1] <- polygon[2,1]
polygon[3,2] <- polygon[2,2]
polygon[3,1] <- polygon[1,1]
polygon <- rbind(polygon, polygon[1,])
Run Code Online (Sandbox Code Playgroud)
看看这看起来是否不错: plot(polygon)
第四步:计算多边形内的面积。
geosphere::areaPolygon(polygon)
[1] 31.99288 #from the help file I know this ought to be square metres.
Run Code Online (Sandbox Code Playgroud)
但是,我可以预料到,200*200=40000 m²因为我的矩形的边长是200 x 200米。可以通过检查
rdist.earth(polygon,coords,miles=FALSE)
[,1] [,2]
[1,] 0.0000000 2.827821e-01
[2,] 0.2827821 …Run Code Online (Sandbox Code Playgroud) 可能重复:
模拟后关闭Windows
我想知道在R中某个进程结束后是否有办法关闭PC?
somefunction()
Sys.shut.down()
Run Code Online (Sandbox Code Playgroud) 我有一个国际格式化数字(即字符串)的文件,包括测量单位.在这种情况下,小数位用","表示,1e3分隔符用"."表示.(即德国数字格式).
a <- c('2.200.222 €',
' 180.109,3 €')
Run Code Online (Sandbox Code Playgroud)
要么
b <- c('28,42 m²',
'47,70 m²')
Run Code Online (Sandbox Code Playgroud)
我想有效地将这些字符串转换为numeric.我试图用代码过滤掉数字
require(stringr)
str_extract(a, pattern='[0-9]+.[0-9]+.[0-9]+')
str_extract(b, pattern='[0-9]+,[0-9]+')
Run Code Online (Sandbox Code Playgroud)
然而,这似乎太容易出错,我想必须有一种更标准化的方式.所以这是我的问题:是否有自定义函数,包或其他能够解决这个问题的东西?
非常感谢你!
我有一个数据框,有2组1时间变量和一个因变量.例如:
name <- c("a", "a", "a", "a", "a", "a","a", "a", "a", "b", "b", "b","b", "b", "b","b", "b", "b")
class <- c("c1", "c1", "c1", "c2", "c2", "c2", "c3", "c3", "c3","c1", "c1", "c1", "c2", "c2", "c2", "c3", "c3", "c3")
year <- c("2010", "2009", "2008", "2010", "2009", "2008", "2010", "2009", "2008", "2010", "2009", "2008", "2010", "2009", "2008", "2010", "2009", "2008")
value <- c(100, 33, 80, 90, 80, 100, 100, 90, 80, 90, 80, 100, 100, 90, 80, 99, 80, 100)
df <- data.frame(name, …Run Code Online (Sandbox Code Playgroud) 给定一组坐标
lat <- c(47.2325618, 47.2328269, 47.2330041, 47.2330481, 47.2330914,
47.2331172, 47.2331291, 47.2331499)
lon <- c(11.3707441, 11.3707791, 11.3708087, 11.3708031, 11.3707818,
11.3707337, 11.3706588, 11.370284)
coords <- cbind(lon,lat)
Run Code Online (Sandbox Code Playgroud)
我想计算多边形的面积.我使用areapl()包splancs中的函数:
library(splancs)
areapl(coords)
# [1] 1.4768e-07
Run Code Online (Sandbox Code Playgroud)
这给我留下了平方度(?)的维度.所以我的问题是:我如何将其转换为米/公里?
非常感谢提前.