2

I am struggling in transforming the data to long format

In R, I have the following data:

alcolevel <- c(0,0.5,1.5,4.0,7.0)
present <- c(48,38,5,1,0)
absent <- c(17066,14464,788,126,37)
dataset1 <- data.frame(alcolevel,present,absent)


     alcolevel present absent 
1         0      48  17066 
2       0.5      38  14464 
3       1.5       5    788   
4       4.0       1    126  
5       7.0       0     37   

I wish to transform dataset1 into this:

alcolevel  Y
0          present
0          present
0          absent
0          absent
0          absent
 .            .
 .            .
 .            .
0.5        absent
0.5        present
0.5        present
0.5        present
 .            .
 .            .
 .            .
1.5        present
 .            .
 .            .
 .            .
7.0        present
7.0        present

so I get 48 present and 17066 absent for alcolevel 0. Same for other alcolevel categories. After the transformation, the new long dataset is gonna be a long one. I appreciate any help with this problem. Thanks

ThePyGuy
  • 17,779
  • 5
  • 18
  • 45
Iwishworldpeace
  • 478
  • 4
  • 12

1 Answers1

2

If we need to replicate, uncount on the 'value' column after pivoting to 'long' format

library(dplyr)
library(tidyr)
dataset1 %>% 
    pivot_longer(cols = -alcolevel, names_to = 'Y' ) %>%
    uncount(value)  %>%
    as_tibble
# A tibble: 32,573 x 2
#   alcolevel    Y  
#       <dbl> <chr>  
# 1         0 present
# 2         0 present
# 3         0 present
# 4         0 present
# 5         0 present
# 6         0 present
# 7         0 present
# 8         0 present
# 9         0 present
#10         0 present
# … with 32,563 more rows
akrun
  • 874,273
  • 37
  • 540
  • 662