Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R how to use pivot wider taking column names from multiple columns, and adding 1 or zero based on presence/absence per sample

I am searching a way for obtaining a large table from a table with a list of species.

Here I give you an example:

I have something like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

data.frame(survey_id = c("ID_1", "ID_2", "ID_3", "ID_4", "ID_5"), 
           list_1 = c("A", "A", "A", "B", "A"), 
           list_2 = c("B", "D", "E", "E", "F"), 
           list_3 = c("C", "", "", "F", ""))

and I want to obtain this

data.frame(survey_id = c("ID_1", "ID_2", "ID_3", "ID_4", "ID_5"), 
           A = c(1,1,1,0,1), 
           B = c(1,0,0,1,0),
           C = c(1,0,0,0,0), 
           D = c(0,1,0,0,0), 
           E = c(0,0,1,1,0), 
           F = c(0,0,0,1,1))

Any suggestion using pivot_wider function?

Thanks for your help.

>Solution :

A slightly round-a-bout way: pivot_longer first, so as you can count by (survey_id, value), and then pivot_wider.

library(dplyr)
library(tidyr)

df1 %>% 
  pivot_longer(-Survey_ID) %>% 
  count(Survey_ID, value) %>% 
  filter(value != "") %>% 
  pivot_wider(names_from = "value", 
              values_from = "n") %>% 
  replace(is.na(.), 0)

Result:

# A tibble: 5 × 7
  Survey_ID     A     B     C     D     E     F
  <chr>     <int> <int> <int> <int> <int> <int>
1 ID_1          1     1     1     0     0     0
2 ID_2          1     0     0     1     0     0
3 ID_3          1     0     0     0     1     0
4 ID_4          0     1     0     0     1     1
5 ID_5          1     0     0     0     0     1

Data df1:

df1 <- data.frame(survey_id = c("ID_1", "ID_2", "ID_3", "ID_4", "ID_5"), 
                  list_1 = c("A", "A", "A", "B", "A"), 
                  list_2 = c("B", "D", "E", "E", "F"), 
                  list_3 = c("C", "", "", "F", ""))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading