R: Collapse multiple boolean columns into single attribute column with new rows for each combination [duplicate]

一个人想着一个人 提交于 2019-12-06 03:29:58
Onyambu

in base R:

 subset(cbind(A=dat[,1],stack(dat[-1])),values==1,-2)
      A ind
1   ex1  S1
4   ex4  S1
7   ex7  S1
10 ex10  S1
12  ex2  S2
14  ex4  S2
15  ex5  S2
16  ex6  S2
17  ex7  S2
18  ex8  S2
23  ex3  S3
27  ex7  S3
28  ex8  S3
29  ex9  S3
35  ex5  S4

In the tidyverse:

library(tidyverse)
dat%>%
   gather(Type,j,-A)%>%
   filter(j==1)%>%
   select(-j)
      A Type
1   ex1   S1
2   ex4   S1
3   ex7   S1
4  ex10   S1
5   ex2   S2
6   ex4   S2
7   ex5   S2
8   ex6   S2
9   ex7   S2
10  ex8   S2
11  ex3   S3
12  ex7   S3
13  ex8   S3
14  ex9   S3
15  ex5   S4
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!