[英]how to cut horizontally long dataframe? - R
This database is from the ggplot2()
package.该数据库来自
ggplot2()
package。 It's not the database I'm using, but it fulfills the minimal example for solving this problem.这不是我使用的数据库,但它满足了解决此问题的最小示例。
When I print df_input
:当我打印
df_input
时:
df_input <-
table(ggplot2::diamonds$cut, ggplot2::diamonds$price) %>%
as.data.frame() %>%
t() %>%
as.data.frame()
Since it is quite long, it is cut horizontally.由于它很长,所以它被水平切割。 But as I need to see the full columns and its data, I'm looking for a way to be able to see it like this (desired output):
但是由于我需要查看完整的列及其数据,我正在寻找一种能够像这样查看它的方法(所需的输出):
df_output <- table(ggplot2::diamonds$cut, ggplot2::diamonds$price)
However, I can't figure out a way to transform df_output
into a table
class (the output for class(df_input)
is table
).但是,我想不出将
df_output
转换为table
class 的方法( class(df_input)
的 output 是table
)。 I was trying to transform it into a list, but things get really hard.我试图将其转换为列表,但事情变得非常困难。 Any suggestions?
有什么建议么?
EDIT: Additional information:编辑:附加信息:
I'm looking to get from df_input
:我希望从
df_input
获得:
To df_output
:至
df_output
:
As you may see above, in the first image you can see the Var2
from 326 to 327 and then data is hidden.正如您在上面看到的,在第一张图片中,您可以看到
Var2
从 326 到 327,然后数据被隐藏。 What's I'm looking to reach is the second image: you can see the Var2
from 326 to 391 (and goes on).我想要达到的是第二张图片:您可以看到
Var2
从 326 到 391(并继续)。
old: I think you want a mix~~ of dplyr::count()
and tidyr::complete()
.老:我想你想要
dplyr::count()
和tidyr::complete()
的混合~~ If not will you explain how it's the desired output is different?如果不是,您会解释所需的 output 有何不同?
library(magrittr)
ggplot2::diamonds %>%
dplyr::count(cut, price) %>%
# tidyr::complete(cut, price, fill=list(n=0L)) %>% # Unnecessary if `values_fill` is specified below
tidyr::pivot_wider(
# names_from = "price",
names_from = "cut",
values_from = "n",
values_fill = list(n=0L)
) %>%
DT::datatable()
Result before transposing & rendering with DT :使用DT转置和渲染之前的结果:
# A tibble: 11,602 x 6
price Fair Good `Very Good` Premium Ideal
<int> <int> <int> <int> <int> <int>
1 337 1 0 1 0 0
2 361 1 2 0 0 1
3 369 1 0 2 0 0
4 371 1 0 0 0 2
5 416 1 1 0 0 1
6 496 1 0 1 0 3
7 497 1 0 1 1 6
8 527 1 0 1 2 2
9 536 1 3 3 3 9
10 563 1 0 1 0 2
# ... with 11,592 more rows
DT in RStudio's Viewer pane: RStudio 的查看器窗格中的 DT:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.