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

How to create multiple columns from a single column in R

I have a large data set in which i have a column which contains 5 different types of values. I want to create 5 different columns( 1 for each value) based on the column. Additionally, each row in those columns should contain either 1 or 0 based on whether another column contained that value
For example suppose this is the data frame

l1=data.frame(c1=  c("A","A","B","B","B","C"), c2 = c("Blue","Green","Red","yellow","Black","Blue"))

The output should be

l2=data.frame(c1=c("A","A","B","B","B","C"), Blue=c(1,0,0,0,0,1),Green=c(0,1,0,0,0,0),Red=c(0,0,1,0,0,0),Yellow=c(0,0,0,1,0,0),Black=c(0,0,0,0,1,0))

Thank you!

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

>Solution :

library(tidyverse)
l1 %>%
  mutate(n = 1, dummy = row_number()) %>%
  pivot_wider(names_from = c2, values_from = n, values_fill = 0) %>%
  select(-dummy)

Result

# A tibble: 6 × 6
  c1     Blue Green   Red yellow Black
  <chr> <dbl> <dbl> <dbl>  <dbl> <dbl>
1 A         1     0     0      0     0
2 A         0     1     0      0     0
3 B         0     0     1      0     0
4 B         0     0     0      1     0
5 B         0     0     0      0     1
6 C         1     0     0      0     0
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