首页 > 解决方案 > R, 3-way table, 如何订购

问题描述

我正在尝试订购一个具有 3 个变量的表,通常称为 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 交手的人。

标签: rdataframe

解决方案


你应该避免在 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 包于 2021-06-01 创建 (v2.0.0 )


推荐阅读