一个简单的问题,但我一直在寻找解决方案,但到目前为止没有任何结果。
假设我有一个列表对象,并且想提取特定的列表项并将其作为数据帧列并排输出。如何以简单的方式使用tidyverse /管道实现此目的?尝试在下面解决它。
数据
some_data <-
structure(list(x = c(23.7, 23.41, 23.87, 24.18, 24.15, 24.31,
23.14, 23.72, 24.12, 23.47, 23.59, 23.29, 23.24, 23.5, 23.56,
23.16, 23.62, 23.67, 23.84, 23.69, 23.7, 23.68, 24.2, 23.77,
23.74, 23.64, 24.39, 24.05, 24.51, 23.6, 24.29, 23.31, 23.96,
24.07, 24.37, 23.77, 23.64, 24, 23.68, 24.02, 23.36, 23.54, 23.34,
23.69, 23.79, 23.8, 23.7, 24.45, 23.27, 23.57, 23.02, 24.23,
23.41, 23.6, 24.02, 23.94, 24.06, 23.97, 23.38, 23.46, 24, 23.89,
23.51, 23.72, 23.83, 23.96, 23.84, 23.52, 24.36, 23.94, 23.82,
24.04, 24.05, 23.6, 23.52, 24.13, 23.43, 23.33, 24.01, 23.99,
24.46, 24.23, 24.19, 23.83, 23.8, 23.93, 23.79, 23.48, 23.26,
24.04, 23.93, 23.98, 23.86, 23.49, 24.17, 23.7, 23.54, 23.55,
23.67, 23.66)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -100L), spec = structure(list(cols = list(
x = structure(list(), class = c("collector_double", "collector"
))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))
我想要此数据的hist()
函数的值输出
library(tidyverse)
some_data$x %>%
as.numeric() %>%
hist(breaks = seq(from = 23, to = 24.6, by = 0.2),
plot = FALSE)
## $breaks
## [1] 23.0 23.2 23.4 23.6 23.8 24.0 24.2 24.4 24.6
## $counts
## [1] 3 9 20 23 19 16 7 3
## $density
## [1] 0.15 0.45 1.00 1.15 0.95 0.80 0.35 0.15
## $mids
## [1] 23.1 23.3 23.5 23.7 23.9 24.1 24.3 24.5
## $xname
## [1] "."
## $equidist
## [1] TRUE
## attr(,"class")
## [1] "histogram"
假设我要同时使用$ breaks和$ counts作为数据框 我将补充原始管道,以便:
some_data$x %>%
as.numeric() %>%
hist(breaks = seq(from = 23, to = 24.6, by = 0.2),
plot = FALSE) %>%
##
map_df(~.[1:30]) %>%
select(bins = breaks,
frequency = counts)
##
## # A tibble: 30 x 2
## bins frequency
## <dbl> <int>
## 1 23 3
## 2 23.2 9
## 3 23.4 20
## 4 23.6 23
## 5 23.8 19
## 6 24 16
## 7 24.2 7
## 8 24.4 3
## 9 24.6 NA
## 10 NA NA
## # ... with 20 more rows
是的,它确实有效,但是map_df()我必须放置一个相对较大的“魔术”数字(任意输入30),以确保包括所有数据。有没有一种更简单的获取方式$breaks和$counts作为数据框?也许甚至只用一个步骤而不是合并map_df()然后select()呢?
谢谢!
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。