1

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?

2 Answers2

3

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")
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
1

Here is another strategy:

  1. assign values to columns with across and case_when
  2. remove number column with .keep="unused"
  3. remove NA's
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
TarJae
  • 72,363
  • 6
  • 19
  • 66