R,3向表格如何排序

3

我想要订购一个具有三个变量的表格,通常被称为三维表。

我已经附上了可复制代码所生成的表格结构图片。

3-waytable

尽管这个表格实质上被分成了三个部分/组,但是否可能以一种逻辑方式对其进行排序?例如,可以根据列“No”或列“Yes”中的值进行排序吗? 例如,当按“ No”排序时,英格兰将按“ Sertosa”(7)、弗吉尼亚(8)、异色鲸(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语言的人。


1
你不能这样做,因为表格的行名称优先级更高。因此,在所有数组中,你将始终具有相同的顺序。 - Onyambu
1个回答

4

在 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 包 (v2.0.0) 于2021年06月01日创建。


they are hard to work with” - [需要引用来源]。对于某些任务来说,表格和数组非常有用且简单,只是可能不适用于这个特定的任务。在表格/数组形式中,您可以执行诸如addmargins(tab)之类的操作,以快速调查分类总和,我甚至无法想象您如何在data.frame中轻松完成此操作。 - thelatemail

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接