【问题标题】:Summarize Multiple Factor Columns Grouped by a Character Column and Return Results as a "Nested" Table in R汇总按字符列分组的多个因子列并将结果作为 R 中的“嵌套”表返回
【发布时间】:2021-09-01 22:57:57
【问题描述】:

我被困在这个问题上,理论上它与this question 比较相似,但在实践中却大不相同,足以让我头疼。

基本上,我有下表:

str(X)
 tibble [50,000 x 8] (S3: tbl_df/tbl/data.frame)
 $ V1          : chr [1:50000] "M" "M" "M" "M" "M" "F" "F" "F" "F" "F" ...
 $ V2          : Factor w/ 5 levels "***","**","*",..: 1 3 1 1 1 1 1 1 1 3 ...
 $ V3          : Factor w/ 5 levels "***","**","*",..: 5 5 4 1 1 1 1 1 1 1 ...
 $ V4          : Factor w/ 5 levels "***","**","*",..: 5 5 5 3 NA 5 5 5 5 5 ...
 $ V5          : Factor w/ 5 levels "***","**","*",..: 1 1 1 1 1 5 4 1 1 1 ...
 $ V6          : Factor w/ 5 levels "***","**","*",..: 2 1 3 1 5 2 5 5 5 5 ...
 $ V7          : Factor w/ 5 levels "***","**","*",..: 1 1 1 1 1 5 1 2 2 1 ...
 $ V8          : Factor w/ 5 levels "***","**","*",..: 1 1 1 NA 1 1 1 1 1 1 ...

我正在尝试获取一个表,该表计算每个变量中每个因子级别的实例并返回下表(由数值组成):

V1    V9      V1      V2      V3      V4      V5       V6      V7      V8
M     ***     323     232     44      445     4455     555     555     555
M     **      5555    6446    444     444     4110     899     8       8444
M     *       323     232     44      445     4455     555     555     555
M     .       5555    6446    444     444     4110     899     8       8444
M     ns      323     232     44      445     4455     555     555     555
F     ***     5555    6446    444     444     4110     899     8       8444
F     **      323     232     44      445     4455     555     555     555
F     *       5555    6446    444     444     4110     899     8       8444
F     .       323     232     44      445     4455     555     555     555
F     ns      5555    6446    444     444     4110     899     8       8444
library(dplyr)
library(tidyr)
X %>%
   pivot_longer(cols = -V1, names_to = "Name", values_to = 'V9') %>%
   count(V1, Name, V9) %>%
   pivot_wider(names_from = Name, values_from = n, values_fill = 0)

根据评论中的要求,我将再次查看数据:

dput(head(X,5))
structure(list(V1 = c("M", "M", "M", 
"M", "M"), V2 = structure(c(1L, 
3L, 1L, 1L, 1L), .Label = c("***", "**", "*", ".", "ns"), class = "factor"), 
    V3 = structure(c(5L, 5L, 4L, 1L, 1L), .Label = c("***", 
    "**", "*", ".", "ns"), class = "factor"), V4 = structure(c(5L, 
    5L, 5L, 3L, NA), .Label = c("***", "**", "*", ".", "ns"), class = "factor"), 
    V5 = structure(c(1L, 1L, 1L, 1L, 1L), .Label = c("***", 
    "**", "*", ".", "ns"), class = "factor"), V6 = structure(c(2L, 
    1L, 3L, 1L, 5L), .Label = c("***", "**", "*", ".", "ns"), class = "factor"), 
    V7 = structure(c(1L, 1L, 1L, 1L, 1L), .Label = c("***", 
    "**", "*", ".", "ns"), class = "factor"), V8 = structure(c(1L, 
    1L, 1L, NA, 1L), .Label = c("***", "**", "*", ".", "ns"), class = "factor")), row.names = c(NA, 
-5L), class = c("tbl_df", "tbl", "data.frame"))

【问题讨论】:

  • 请使用dput()dput(head(YourData, 30))分享您的数据。
  • @MartinGal,我已经包含了数据。尽管如此,akrun 已经提供了一个可行的解决方案。

标签: r dplyr aggregate tidyverse


【解决方案1】:

一种选择是使用pivot_longer 重新整形为“长”格式,获取计数并重新整形

library(dplyr)
library(tidyr)
X %>%
   pivot_longer(cols = -V1, names_to = "Name", values_to = 'V9') %>%
   count(V1, Name, V9) %>%
   pivot_wider(names_from = Name, values_from = n, values_fill = 0)

数据

X <- structure(list(V1 = c("M", "M", "M", 
"M", "M"), V2 = structure(c(1L, 
3L, 1L, 1L, 1L), .Label = c("***", "**", "*", ".", "ns"), class = "factor"), 
    V3 = structure(c(5L, 5L, 4L, 1L, 1L), .Label = c("***", 
    "**", "*", ".", "ns"), class = "factor"), V4 = structure(c(5L, 
    5L, 5L, 3L, NA), .Label = c("***", "**", "*", ".", "ns"), class = "factor"), 
    V5 = structure(c(1L, 1L, 1L, 1L, 1L), .Label = c("***", 
    "**", "*", ".", "ns"), class = "factor"), V6 = structure(c(2L, 
    1L, 3L, 1L, 5L), .Label = c("***", "**", "*", ".", "ns"), class = "factor"), 
    V7 = structure(c(1L, 1L, 1L, 1L, 1L), .Label = c("***", 
    "**", "*", ".", "ns"), class = "factor"), V8 = structure(c(1L, 
    1L, 1L, NA, 1L), .Label = c("***", "**", "*", ".", "ns"), class = "factor")), row.names = c(NA, 
-5L), class = c("tbl_df", "tbl", "data.frame"))

【讨论】:

  • 这正是我所需要的!!!非常感谢!我会在 9 分钟内接受答案(由于时间缓冲)。
  • 谢谢@akrun,我本想编辑问题,但不小心弄乱了你的答案。
  • @FranjoIM 没关系。我回滚了编辑。感谢提醒
猜你喜欢
  • 2016-11-15
  • 2018-11-27
  • 2020-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-07
  • 2023-04-03
  • 1970-01-01
相关资源
最近更新 更多