I'm looking for a way to utilize dplyr
's group_by
functionality to make counts, and a plot-less histogram for mpg
after grouping_by gear
and vs
.
我正在寻找一种方法来利用dplyr的group_by功能进行计数,以及在grouping_by gear和vs.之后使用mpg的无绘图直方图
My code is:
我的代码是:
mtcars %>%
group_by(gear,vs) %>%
summarise(counts = count (n), hist(mpg, plot = FALSE, breaks = c(seq(10,40,1))))
The error is:
错误是:
Error in summarise_impl(.data, dots) : Column hist(mpg, plot = FALSE, breaks = c(seq(10, 40, 1)))` must be length 1 (a summary value), not 6
summarise_impl(.data,dots)出错:列hist(mpg,plot = FALSE,breaks = c(seq(10,40,1)))`必须是长度1(摘要值),而不是6
I'm not constrained to dplyr
but that's all I'm familiar with in R at this point.
我并不局限于dplyr,但这就是我在R中所熟悉的。
Any help is appreciated.
任何帮助表示赞赏。
2 个解决方案
#1
2
I am not really sure what a plotless histogram is, but does this help?
我不确定无量纲直方图是什么,但这有帮助吗?
mtcars %>%
mutate(mpgClasses = cut(mpg, 10:40)) %>%
group_by(gear, vs, mpgClasses) %>%
summarise(n())
You can also rearrange that a little like this
你也可以重新安排一下这样的事情
mtcars %>%
mutate(mpgClasses = cut(mpg, 10:40)) %>%
group_by(gear, vs, mpgClasses) %>%
summarise(counts = n()) %>%
spread(mpgClasses, counts)
If you can describe a little more, what you are heading for we can find a better solution.
如果你能描述一下,你前进的目标是什么,我们可以找到更好的解决方案。
#2
0
Here, I'm just extracting the counts
from the hist
. Since I have to make it one element for each group, I made it a list.
在这里,我只是从hist中提取计数。因为我必须为每个组制作一个元素,所以我把它作为一个列表。
library(dplyr)
x <- mtcars %>% group_by(gear,vs) %>%
summarise(counts = n(),
hcounts = list(hist(mpg, plot = FALSE, breaks = c(seq(10,40,1)))$counts))
x
# # A tibble: 6 x 4
# # Groups: gear [?]
# gear vs counts hcounts
# <dbl> <dbl> <int> <list>
# 1 3 0 12 <int [30]>
# 2 3 1 3 <int [30]>
# 3 4 0 2 <int [30]>
# 4 4 1 10 <int [30]>
# 5 5 0 4 <int [30]>
# 6 5 1 1 <int [30]>
x$hcounts
# [[1]]
# [1] 2 0 0 1 2 3 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#
# [[2]]
# [1] 0 0 0 0 0 0 0 0 1 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#
# [[3]]
# [1] 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#
# [[4]]
# [1] 0 0 0 0 0 0 0 1 0 1 0 1 2 0 1 0 0 1 0 0 1 0 1 1 0 0 0 0 0 0
#
# [[5]]
# [1] 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#
# [[6]]
# [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
#1
2
I am not really sure what a plotless histogram is, but does this help?
我不确定无量纲直方图是什么,但这有帮助吗?
mtcars %>%
mutate(mpgClasses = cut(mpg, 10:40)) %>%
group_by(gear, vs, mpgClasses) %>%
summarise(n())
You can also rearrange that a little like this
你也可以重新安排一下这样的事情
mtcars %>%
mutate(mpgClasses = cut(mpg, 10:40)) %>%
group_by(gear, vs, mpgClasses) %>%
summarise(counts = n()) %>%
spread(mpgClasses, counts)
If you can describe a little more, what you are heading for we can find a better solution.
如果你能描述一下,你前进的目标是什么,我们可以找到更好的解决方案。
#2
0
Here, I'm just extracting the counts
from the hist
. Since I have to make it one element for each group, I made it a list.
在这里,我只是从hist中提取计数。因为我必须为每个组制作一个元素,所以我把它作为一个列表。
library(dplyr)
x <- mtcars %>% group_by(gear,vs) %>%
summarise(counts = n(),
hcounts = list(hist(mpg, plot = FALSE, breaks = c(seq(10,40,1)))$counts))
x
# # A tibble: 6 x 4
# # Groups: gear [?]
# gear vs counts hcounts
# <dbl> <dbl> <int> <list>
# 1 3 0 12 <int [30]>
# 2 3 1 3 <int [30]>
# 3 4 0 2 <int [30]>
# 4 4 1 10 <int [30]>
# 5 5 0 4 <int [30]>
# 6 5 1 1 <int [30]>
x$hcounts
# [[1]]
# [1] 2 0 0 1 2 3 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#
# [[2]]
# [1] 0 0 0 0 0 0 0 0 1 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#
# [[3]]
# [1] 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#
# [[4]]
# [1] 0 0 0 0 0 0 0 1 0 1 0 1 2 0 1 0 0 1 0 0 1 0 1 1 0 0 0 0 0 0
#
# [[5]]
# [1] 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#
# [[6]]
# [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0