简体   繁体   中英

how to cut horizontally long dataframe? - R

This database is from the 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 <- 
  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 ). 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 :

在此处输入图像描述

To 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. What's I'm looking to reach is the second image: you can see the Var2 from 326 to 391 (and goes on).

old: I think you want a mix~~ of dplyr::count() and tidyr::complete() . If not will you explain how it's the desired output is different?

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 :

# 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:

截图-dt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM