小编Jor*_*dal的帖子

dplyr:在NSE中按位置选择列

我正在尝试创建一个函数,根据它们的位置选择DF中的列.我将始终需要第一列,然后是DF的子集.我需要为每个子集选择一个对象.

到目前为止,我尝试了以下内容:

position <- "1,28:31"
DF %>%
  select_(.dots = position)
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

解析时出错(text = x):: 1:2:意外'x'

似乎问题是列位置的逗号分离.

有解决方法吗?

select r dplyr nse

10
推荐指数
3
解决办法
1万
查看次数

使用facetted ggplot 2.0.0和gridExtra排列公共绘图宽度

由于我已经更新到ggplot2 2.0.0,我无法使用gridExtra正确排列图表.问题是分面图表会被压缩,而其他图表会扩展.宽度基本搞砸了.我想安排它们类似于这些单面图的方式:左对齐两个图形边(ggplot)

我把一个可重现的代码

library(grid) # for unit.pmax()
library(gridExtra)

plot.iris <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + 
  geom_point() + 
  facet_grid(. ~ Species) + 
  stat_smooth(method = "lm")

plot.mpg <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) + 
  geom_point(size=2.5)

g.iris <- ggplotGrob(plot.iris) # convert to gtable
g.mpg <- ggplotGrob(plot.mpg) # convert to gtable

iris.widths <- g.iris$widths # extract the first three widths, 
mpg.widths <- g.mpg$widths # same for mpg plot
max.widths <- unit.pmax(iris.widths, mpg.widths)

g.iris$widths <- max.widths # assign max. widths …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gridextra gtable

6
推荐指数
2
解决办法
3618
查看次数

R dplyr 将多个函数总结为选定的变量

我有一个数据集,我想对其进行平均值总结,但也计算其中 1 个变量的最大值。

\n\n

让我从一个我想要实现的目标开始:

\n\n
iris %>%\n  group_by(Species) %>%\n  filter(Sepal.Length > 5) %>%\n  summarise_at("Sepal.Length:Petal.Width",funs(mean))\n
Run Code Online (Sandbox Code Playgroud)\n\n

这给了我以下结果

\n\n
# A tibble: 3 \xc3\x97 5\n     Species Sepal.Length Sepal.Width Petal.Length Petal.Width\n      <fctr>        <dbl>       <dbl>        <dbl>       <dbl>\n1     setosa          5.8         4.4          1.9         0.5\n2 versicolor          7.0         3.4          5.1         1.8\n3  virginica          7.9         3.8          6.9         2.5\n
Run Code Online (Sandbox Code Playgroud)\n\n

有没有简单的方法来添加,例如max(Petal.Width)总结?

\n\n

到目前为止,我已经尝试过以下方法:

\n\n
iris %>%\n  group_by(Species) %>%\n  filter(Sepal.Length > 5) %>%\n  summarise_at("Sepal.Length:Petal.Width",funs(mean)) %>%\n  mutate(Max.Petal.Width = max(iris$Petal.Width))\n
Run Code Online (Sandbox Code Playgroud)\n\n

但通过这种方法,我丢失了上面代码中的group_by和,并给出了错误的结果。filter

\n\n

我能够实现的唯一解决方案如下:

\n\n
iris %>%\n  group_by(Species) …
Run Code Online (Sandbox Code Playgroud)

r dplyr summarize

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

r ×3

dplyr ×2

ggplot2 ×1

gridextra ×1

gtable ×1

nse ×1

select ×1

summarize ×1