I have a boolean table of four columns in a dataframe with some other fields e.g.
A B C D X
---------
1 T T F T 0.1
2 F T F F 0.2
3 F F T T 0.3
There are more than one field X but let's just use X for this example.
How can I make a new table of duplicated rows by column names?
Type X
-------
1 A 0.1
1 B 0.1
1 D 0.1
2 B 0.2
3 C 0.3
3 D 0.3
library(tidyr)
library(dplyr)
pivot_longer(df, cols=A:D, names_to="Type") |>
filter(value) |>
select(-value)
# A tibble: 6 × 3
id X Type
<dbl> <dbl> <chr>
1 1 0.1 A
2 1 0.1 B
3 1 0.1 D
4 2 0.2 B
5 3 0.3 C
6 3 0.3 D
Data:
df <- structure(list(id = c(1, 2, 3), A = c(TRUE, FALSE, FALSE), B = c(TRUE,
TRUE, FALSE), C = c(FALSE, FALSE, TRUE), D = c(TRUE, FALSE, TRUE
), X = c(0.1, 0.2, 0.3)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -3L))