我正在尝试ggplot2使用来自不同数据帧的数据构建映射.
library(maptools)
xx <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1], IDvar="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))
xx.sub1 <- subset(xx, xx$FIPSNO < 37010)
xx.sub2 <- subset(xx, xx$FIPSNO > 37010)
xx.sub1@data$id <- rownames(xx.sub1@data)
xx.sub1.points <- fortify(xx.sub1, region="id")
xx.sub1.df = plyr::join(xx.sub1.points, xx.sub1@data, by="id")
xx.sub2@data$id <- rownames(xx.sub2@data)
xx.sub2.points <- fortify(xx.sub2, region="id")
xx.sub2.df = plyr::join(xx.sub2.points, xx.sub2@data, by="id")
ggplot(xx.sub2.df) +
aes(long, lat, fill = (SID79/BIR79)*1000, group = group) +
geom_polygon() + geom_path(color="grey80") +
coord_equal() +
scale_fill_gradientn(colours = RColorBrewer::brewer.pal(7, "YlOrBr")) +
geom_polygon(data = xx.sub1.df, fill = "grey50") +
geom_path(data = xx.sub1.df, color="grey80") …Run Code Online (Sandbox Code Playgroud) 如何根据各行的条件选择组,例如过滤包含值4(或任何其他条件)的所有组.
让我们采用一个非常简单的数据,有两组,我想选择B组(值为4)
library(dplyr)
df <- data.frame(Group=LETTERS[c(1,1,1,2,2,2)], Value=c(1:5,4))
> df
Group Value
1 A 1
2 A 2
3 B 3
4 B 4
Run Code Online (Sandbox Code Playgroud)
执行group_by()然后filter(如在此帖子中)将仅选择包含值4的单个行,而不是整个组:
df %>%
group_by(Group) %>%
filter(Value==4)
Group Value
<fctr> <int>
1 B 4
Run Code Online (Sandbox Code Playgroud) 我purrr:::pmap用三个输入R.目前尚不清楚我如何在公式调用中明确引用这些输入?使用map2时,公式调用如下~ .x + .y.但使用时该怎么办pmap?
从http://r4ds.had.co.nz/lists.html转载Hadley的例子
library(purrr)
mu <- list(5, 10, -3)
sigma <- list(1, 5, 10)
n <- list(1, 3, 5)
args2 <- list(mean = mu, sd = sigma, n = n)
pmap(args2, rnorm)
Run Code Online (Sandbox Code Playgroud)
如果我想在调用时显式引用输入参数rnorm,我可以使用:
pmap(args2, function(mean, sd, n) rnorm(n, mean, sd))
Run Code Online (Sandbox Code Playgroud)
但是说我想用公式方法做到这一点.我怎么做?例如,这不起作用:
pmap(args2, ~rnorm(n=.n, mean=.mean, sd=.sd))
Run Code Online (Sandbox Code Playgroud)
谢谢!!
是%>%管道运营商不断将左手侧(LHS)到右侧(RHS)的第一个参数?即使在RHS调用中再次指定第一个参数?
假设我想指定要使用的变量cor():
library(magrittr)
iris %>%
cor(x=.$Sepal.Length, y=.$Sepal.Width)
Run Code Online (Sandbox Code Playgroud)
但这失败了,它看起来像是什么样的cor(., x=.$Sepal.Length, y=.$Sepal.Width)?
我知道我可以用
iris %$%
cor(x=Sepal.Length, y=Sepal.Width)
Run Code Online (Sandbox Code Playgroud)
但想找到一个解决方案%>%......
我试图找出哪些对象在我的 R 会话中占用了大量内存,但问题是该对象可能是在未知环境中以未知名称无形创建的。
如果对象是存储在.GlobalEnv或已知的环境中,我可以很容易地使用像一个策略ls(enviro)+get()+object.size()(参见lsos上这个职位为例)列出的所有对象和它们的大小,让我找出重物。
但是,有问题的对象可能不会存储在 中.GlobalEnv,而可能位于由外部包隐式创建的某个模糊环境中。在这种情况下,如何确定哪个对象使用了大量 RAM?
最好的案例研究是在专用环境中ggplot2创建.last_plot。在引擎盖下查看可以发现它存储在 中environment(ggplot2:::.store$get),因此可以找到它并最终将其删除。但是如果我不知道那个位置或先验的名字,有没有办法.last_plot在内存中找到一个叫做某处的重物?
pryr::mem_used()
#> 34.7 MB
## example: implicit creation of heavy and hidden object by ggplot
path <- tempfile()
if(!file.exists(path)){
saveRDS(as.data.frame(matrix(rep(1,1e07), ncol=5)), path)
}
pryr::mem_used()
#> 34.9 MB
p1 <- ggplot2::ggplot(readr::read_rds(path), ggplot2::aes(V1))
rm(p1)
pryr::mem_used()
#> 127 MB
## Hidden object is not in .GlobalEnv
ls(.GlobalEnv, all.names = TRUE)
#> [1] "path"
## Here I …Run Code Online (Sandbox Code Playgroud) 我想要一个灵活的函数,summarize其中使用:
一个很好的例子是用户提供fun=weighted.mean()并指定权重参数w。
现在,我正在尝试使用.... 问题是我找不到一种方法来...引用数据框中的变量?下面的示例是使用 给出的across(),但如果我使用 ,也会发生同样的情况summarize_at()
谢谢!!
\nlibrary(tidyverse)\nfo1 <- function(df, fun=mean, ...){\n df %>% \n group_by(Species) %>% \n summarise(across(starts_with("sepal"), fun, ...))\n}\n\nfo1(iris)\n#> `summarise()` ungrouping output (override with `.groups` argument)\n#> # A tibble: 3 x 3\n#> Species Sepal.Length Sepal.Width\n#> <fct> <dbl> <dbl>\n#> 1 setosa 5.01 3.43\n#> 2 versicolor 5.94 2.77\n#> 3 virginica 6.59 2.97\nfo1(iris, fun=weighted.mean)\n#> `summarise()` ungrouping output (override …Run Code Online (Sandbox Code Playgroud) 我试图简单地复制示例rvest::html_nodes(),但遇到错误:
library(rvest)
ateam <- read_html("http://www.boxofficemojo.com/movies/?id=ateam.htm")
html_nodes(ateam, "center")
Run Code Online (Sandbox Code Playgroud)
do.call中的错误(方法,列表(parsed_selector)):找不到函数"xpath_element"
同样的情况,如果我打开包,例如httr,xml2,selectr.我似乎也有这些软件包的最新版本......
在该包的功能,例如xpath_element,xpath_combinedselector在什么位置?我如何让它工作?请注意,我在Ubuntu 16.04上运行,因此该代码可能适用于其他平台...
我主要在fromat 的小标题中使用表格tidyverse,但是对于某些步骤,我使用了data.table包装。我想看到的是转换的最佳途径data.table回tibble?
我知道它data.table具有一些巧妙的功能setDT和setDF功能,它们可以通过引用从data.frame转换为data.table(反之亦然),即无需复制。
但是,如果我想转换回小标题怎么办?我使用复制数据as_tibble的data.frame从产生的setDT()?是否有聪明的方法可以使用此方法,也许可以使用setattr()from方法data.table?
library(data.table)
library(tidyverse)
iris_tib <- as_tibble(iris)
## some data.table operation
setDT(iris_tib)
setkey(iris_tib, Species)
iris_tib[, Sepal.Length.Mean := mean(Sepal.Length), by = Species]
## How to convert back to tibble efficiently?
setDF(iris_tib)
iris_tib_back <- as_tibble(iris_tib)
## it looks like we were able to update by reference? Only rownames …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个自定义curve函数,其中...将传递给函数而不是绘图:我希望能够使用说:
curve2(dnorm, mean=2, sd=3)
Run Code Online (Sandbox Code Playgroud)
...我在处理环境时遇到了问题call。从以下的简化原型开始curve:
minicurve <- function (expr, from = 0, to = 1, ...)
{
sexpr <- substitute(expr)
expr <- call(as.character(sexpr), as.name("x"))
ll <- list(x = seq(from=from, to=to, length.out=100))
names(ll) <- "x"
y <- eval(expr, envir = ll, enclos = parent.frame())
plot(x = ll$x, y = y, type="l")
}
# This gives the same behaviour as `curve`:
minicurve(dnorm)
Run Code Online (Sandbox Code Playgroud)
现在我想将 the 传递...到 the call(而不是传递到plot)。通常,这非常简单,只需将 传递 …
我想replicate()使用省略号在函数中包装一个调用.说:
fo() 有2个参数: fo <- function(x, times) x * timesreplicate()将通过第一个名称,第二个使用....
rep_it <- function(N, ...) replicate(N, fo(x=3, ...))事实证明,复制似乎是传递0值而不是传递第二个参数?
fo <- function(x, times) x * times
rep_it <- function(N, ...) replicate(N, fo(x=3, ...))
rep_it(5, times = 4) # should return 5 times 3 * 4 = 12
#> [1] 0 0 0 0 0
Run Code Online (Sandbox Code Playgroud)
这似乎是由于省略号!如果我要说出这个论点,那就没问题了:
rep_it2 <- function(N, times) replicate(N, fo(x=3, times))
rep_it2(5, times = 4)
#> [1] 12 12 12 12 12
Run Code Online (Sandbox Code Playgroud)
为什么会发生这种情况,以及如何处理它?我看到replicate() …