将元素和子元素表分解为一个包含所有可能排列的大表

时间:2017-08-04 11:45:37

标签: r

模糊标题的应用,不知道如何描述我正在尝试做的事情......这个例子将非常清楚,所以它就是这样。

require (tibble)

#I have a (much larger) table of items that I need to turn into a very large lookup table  
ElementMatrix <- tribble(
  ~Category, ~Elements,
  "Gender",   "Male", 
  "Gender",   "Female", 
  "Smoking",   "Smoker", 
  "Smoking",   "Non-Smoker", 
  "Type1",   "A", 
  "Type1",   "B", 
  "Type1",   "C",
  "Type1",   NA
)

#into this
BigLookupMatrix <- tribble(
  ~Gender, ~Smoking, ~Type1,
  "Male", "Smoker", "A",
  "Male", "Smoker", "B",
  "Male", "Smoker", "C",
  "Male", "Smoker", NA,
  "Male", "Non-Smoker", "A",
  "Male", "Non-Smoker", "B",
  "Male", "Non-Smoker", "C",
  "Male", "Non-Smoker", NA,
  "Female", "Smoker", "A",
  "Female", "Smoker", "B",
  "Female", "Smoker", "C",
  "Female", "Smoker", NA,
  "Female", "Non-Smoker", "A",
  "Female", "Non-Smoker", "B",
  "Female", "Non-Smoker", "C",
  "Female", "Non-Smoker", NA
)

#I guessed it would be sonme gather / spready type thing, but that clearly doesnt work
gather(ElementMatrix, key=Category, value=Elements) #gives me back my origitional matrix
spread(ElementMatrix, key=Category, value=Elements) #gets angry about Duplicate identifiers

现在,我显然可以做几个嵌套循环,但这看起来非常混乱。必须有一个漂亮而干净的方法来做到这一点。

非常感谢提前帮助!

2 个答案:

答案 0 :(得分:3)

unstackexpand.grid的基础R怎么样?

expand.grid(unstack(ElementMatrix, Elements ~ Category))
   Gender    Smoking Type1
1    Male     Smoker     A
2  Female     Smoker     A
3    Male Non-Smoker     A
4  Female Non-Smoker     A
5    Male     Smoker     B
6  Female     Smoker     B
7    Male Non-Smoker     B
8  Female Non-Smoker     B
9    Male     Smoker     C
10 Female     Smoker     C
11   Male Non-Smoker     C
12 Female Non-Smoker     C
13   Male     Smoker  <NA>
14 Female     Smoker  <NA>
15   Male Non-Smoker  <NA>
16 Female Non-Smoker  <NA>

unstack将按类别拆分您的Elements列,此处返回一个命名列表。这被馈送到expand.grid,它生成一个data.frame,其中包含三元组的所有组合(Gender-Smoking-Type1)。

答案 1 :(得分:1)

您也可以在tidyverse

中执行此操作
library(tidyverse)

ElementMatrix %>%
  group_by(Category) %>%
  summarise(Elements = list(Elements)) %>%
  spread(Category, Elements) %>%
  as.list() %>% 
  transpose() %>% 
  flatten() %>% 
  expand.grid() %>% 
  arrange(Gender, Smoking, Type1)