簡體   English   中英

橫長dataframe怎么剪? - R

[英]how to cut horizontally long dataframe? - R

該數據庫來自ggplot2() package。 這不是我使用的數據庫,但它滿足了解決此問題的最小示例。

當我打印df_input時:

df_input <- 
  table(ggplot2::diamonds$cut, ggplot2::diamonds$price) %>%
  as.data.frame() %>%
  t() %>% 
  as.data.frame()

由於它很長,所以它被水平切割。 但是由於我需要查看完整的列及其數據,我正在尋找一種能夠像這樣查看它的方法(所需的輸出):

df_output <- table(ggplot2::diamonds$cut, ggplot2::diamonds$price)

但是,我想不出將df_output轉換為table class 的方法( class(df_input)的 output 是table )。 我試圖將其轉換為列表,但事情變得非常困難。 有什么建議么?

編輯:附加信息:

我希望從df_input獲得:

在此處輸入圖像描述

df_output

在此處輸入圖像描述

正如您在上面看到的,在第一張圖片中,您可以看到Var2從 326 到 327,然后數據被隱藏。 我想要達到的是第二張圖片:您可以看到Var2從 326 到 391(並繼續)。

老:我想你想要dplyr::count()tidyr::complete()的混合~~ 如果不是,您會解釋所需的 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()

使用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

RStudio 的查看器窗格中的 DT:

截圖-dt

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM