r - R, 3-way table, 如何订购
问题描述
我正在尝试订购一个具有 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 中使用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 )
推荐阅读
- python - 通过python应用循环更改列名和拆分文本
- mysql - MySQL 内部连接查询显示重复条目
- spring-mvc - 在移动应用程序中进行外部重定向后 Spring MVC 会话丢失
- java - 当条件为假时Java do while循环不会停止
- powershell - Start-Process with PowerShell.exe 表现出嵌入单引号和双引号的不同行为
- html - Bootstrap 4 - 将页脚推到页面底部并用主要内容填充高度
- ms-access - 在 Access 中的传递查询中使用文本框中的值作为参数
- c - 字符串变量赋值给出指针错误
- typescript - React Navigation 4.x tabBarOptions 不工作打字稿
- reactjs - React 中“无法在现有状态转换期间更新(例如在`render`中)”错误的解决方案是什么?