【问题标题】:R, 3-way table, how to orderR, 3-way table, 如何订购
【发布时间】:2021-06-02 04:07:59
【问题描述】:

我正在尝试订购一个有 3 个变量的表格,通常称为 3 路表格。

我附上了一张可复制代码将产生的表格结构的图片。

是否可以以合乎逻辑的方式对该表进行排序,尽管它实际上分为三个部分/组?例如,您可以根据值按“否”列或“是”列排序吗? 例如,在订购“No”时,England 将被订购为“Sertosa”(7)、Virginica(8)、Versicolour(16)。威尔士将被订购 Versicolor (11)、Setoda (12)、Virginica... 等等。

#使用 R 中内置的 Iris 数据的可复制代码:

Data <- iris
Data $ var2 <- Data $ Species
Data $ var2 <- sample(Data $ var2)
Data $ var3 <- Data $ Species
Data $ var3 <- sample(Data $ var3)
#making the example clearer
library(plyr)
Data $ var2 <- revalue(Data $ var2, c("setosa"="No", "versicolor"="No","virginica" ="Yes")) 
Data $ var3 <- revalue(Data $ var3, c("setosa"="England", "versicolor"="Wales","virginica" ="Scotland")) 
#3-way Table:
df <- table(Data $ Species, Data $ var2, Data $ var3)
df

亲切的问候,詹姆斯·普伦蒂斯,一个试图与 R 交手的人。

【问题讨论】:

  • 你不能因为使用表格,行名优先。因此,您将始终在所有数组中具有相同的顺序。

标签: r dataframe


【解决方案1】:

您应该避免在 R 中使用 table()array(),因为它们很难使用。另外,我建议您专注于学习 dplyr,而不是 plyr,因为不再维护 plyr

不使用table(),直接使用原始数据框:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
Data <- iris
Data$Living <- sample(c("No", "Yes"), size = nrow(Data), replace = TRUE)
Data$Country <- sample(c("England", "Wales", "Scotland"), size = nrow(Data), replace = TRUE)

# Results in one data frame
Data %>%
  group_by(Country, Species) %>%
  summarize(Yes = sum(Living == "Yes"), No = sum(Living == "No")) %>%
  ungroup() %>% 
  arrange(Country, Yes) 
#> `summarise()` has grouped output by 'Country'. You can override using the `.groups` argument.
#> # A tibble: 9 x 4
#>   Country  Species      Yes    No
#>   <chr>    <fct>      <int> <int>
#> 1 England  virginica      2     8
#> 2 England  versicolor     7    15
#> 3 England  setosa        14     5
#> 4 Scotland setosa         5    14
#> 5 Scotland virginica      6    12
#> 6 Scotland versicolor     9     8
#> 7 Wales    setosa         4     8
#> 8 Wales    versicolor     5     6
#> 9 Wales    virginica     14     8

# Results in a list of data frames 
Data %>%
  group_by(Country, Species) %>%
  summarize(Yes = sum(Living == "Yes"), No = sum(Living == "No")) %>%
  ungroup() %>% 
  arrange(Country, Yes) %>%
  split(., .$Country)
#> `summarise()` has grouped output by 'Country'. You can override using the `.groups` argument.
#> $England
#> # A tibble: 3 x 4
#>   Country Species      Yes    No
#>   <chr>   <fct>      <int> <int>
#> 1 England virginica      2     8
#> 2 England versicolor     7    15
#> 3 England setosa        14     5
#> 
#> $Scotland
#> # A tibble: 3 x 4
#>   Country  Species      Yes    No
#>   <chr>    <fct>      <int> <int>
#> 1 Scotland setosa         5    14
#> 2 Scotland virginica      6    12
#> 3 Scotland versicolor     9     8
#> 
#> $Wales
#> # A tibble: 3 x 4
#>   Country Species      Yes    No
#>   <chr>   <fct>      <int> <int>
#> 1 Wales   setosa         4     8
#> 2 Wales   versicolor     5     6
#> 3 Wales   virginica     14     8

reprex package (v2.0.0) 于 2021-06-01 创建

【讨论】:

  • 他们很难相处” - [需要引用]。表和数组对于某些 任务非常有用且简单,但可能不是这个特定的任务。在表格/数组形式中,您可以执行 addmargins(tab) 之类的操作来快速调查分类总和,我什至无法想象您将如何像在 data.frame 中那样简单地进行操作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-17
  • 2017-10-20
  • 1970-01-01
  • 2012-03-25
相关资源
最近更新 更多