I have a data frame that looks like this:
number | A | B | C | D |
---|---|---|---|---|
0.3 | 0 | 1 | 0 | 1 |
0.4 | 1 | 1 | 1 | 0 |
and I want to have this data frame:
number | category |
---|---|
0.3 | B |
0.3 | D |
0.4 | A |
0.4 | B |
0.4 | C |
Is there any way or function that can help me to do this?
I have a data frame that looks like this:
number | A | B | C | D |
---|---|---|---|---|
0.3 | 0 | 1 | 0 | 1 |
0.4 | 1 | 1 | 1 | 0 |
and I want to have this data frame:
number | category |
---|---|
0.3 | B |
0.3 | D |
0.4 | A |
0.4 | B |
0.4 | C |
Is there any way or function that can help me to do this?
You can use pivot_longer
to get the data in long format and filter
the values that are not 0.
library(dplyr)
library(tidyr)
df %>%
pivot_longer(cols = -number,
names_to = 'category') %>%
filter(value != 0) %>%
select(-value)
# number category
# <dbl> <chr>
#1 0.3 B
#2 0.3 D
#3 0.4 A
#4 0.4 B
#5 0.4 C
data
It is easier to help if you provide data in reproducible format.
df <- structure(list(number = c(0.3, 0.4), A = 0:1, B = c(1L, 1L),
C = 0:1, D = 1:0), row.names = c(NA, -2L), class = "data.frame")
Here is another strategy:
across
and case_when
number
column with .keep="unused"
library(dplyr)
librayr(tidyr)
df %>%
mutate(across(A:D, ~case_when(. == 1 ~ as.numeric(number))), .keep="unused") %>%
pivot_longer(
cols = A:D,
names_to = "category",
values_to = "number"
) %>%
na.omit()
output:
# A tibble: 5 x 2
category number
<chr> <dbl>
1 B 0.3
2 D 0.3
3 A 0.4
4 B 0.4
5 C 0.4