# How to transform each column's values ​into columns containing only `0` or `1` using dplyr?

## Issue

I have is iris[1:4]

get all the unique values ​​between iris [1:4]

``````as.vector(unlist(iris[,c(1:4,-5)]) %>% unique() %>% sort() -> res
``````

build a new matrix

``````Data<-matrix(0,
nrow=dim(iris)[1],
ncol=length(res),
dimnames = list(1:dim(iris)[1],paste(res))
)
``````

I know this is not the most efficient and generic way to do this process:

``````ifelse(colnames(Data)[1] == iris[1,4],Data[1,1]<-1,Data[1,1]<-0)
ifelse(colnames(Data)[1] == iris[2,4],Data[2,1]<-1,Data[2,1]<-0)
...
ifelse(colnames(Data)[1] == iris[132,4],Data[132,1]<-1,Data[132,1]<-0)
ifelse(colnames(Data)[1] == iris[149,4],Data[149,1]<-1,Data[149,1]<-0)
ifelse(colnames(Data)[1] == iris[150,4],Data[150,1]<-1,Data[150,1]<-0)
####
ifelse(colnames(Data)[2] == iris[1,3],Data[1,2]<-1,Data[1,2]<-0)
ifelse(colnames(Data)[2] == iris[2,3],Data[2,2]<-1,Data[2,2]<-0)
...
ifelse(colnames(Data)[2] == iris[132,3],Data[132,2]<-1,Data[132,2]<-0)
ifelse(colnames(Data)[2] == iris[149,3],Data[149,2]<-1,Data[149,2]<-0)
ifelse(colnames(Data)[2] == iris[150,3],Data[150,2]<-1,Data[150,2]<-0)
....
``````

Objective: Develop a genetic algorithm using dplyr to generate the:

expected output:

## Solution

Use `pivot_longer()` then `pivot_wider()`:

``````library(tidyverse)
data = iris[, 1:4]
data %>%
mutate(id = 1:n()) %>%
pivot_longer(-id) %>%
mutate(ind = 1) %>%
arrange(value) %>%
pivot_wider(
id_cols = "id",
names_from = "value",
values_from = "ind"
) %>%
mutate(across(everything(), replace_na, replace = 0)) %>%
arrange(id)
``````

Output

``````# A tibble: 150 x 75
id `0.1` `0.2` `0.3` `0.4` `0.5` `0.6`   `1` `1.1` `1.2` `1.3`
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1     1     0     1     0     0     0     0     0     0     0     0
2     2     0     1     0     0     0     0     0     0     0     0
3     3     0     1     0     0     0     0     0     0     0     1
4     4     0     1     0     0     0     0     0     0     0     0
5     5     0     1     0     0     0     0     0     0     0     0
6     6     0     0     0     1     0     0     0     0     0     0
7     7     0     0     1     0     0     0     0     0     0     0
8     8     0     1     0     0     0     0     0     0     0     0
9     9     0     1     0     0     0     0     0     0     0     0
10    10     1     0     0     0     0     0     0     0     0     0
# ... with 140 more rows, and 64 more variables: 1.4 <dbl>,
#   1.5 <dbl>, 1.6 <dbl>, 1.7 <dbl>, 1.8 <dbl>, 1.9 <dbl>, 2 <dbl>,
#   2.1 <dbl>, 2.2 <dbl>, 2.3 <dbl>, 2.4 <dbl>, 2.5 <dbl>, 2.6 <dbl>,
#   2.7 <dbl>, 2.8 <dbl>, 2.9 <dbl>, 3 <dbl>, 3.1 <dbl>, 3.2 <dbl>,
#   3.3 <dbl>, 3.4 <dbl>, 3.5 <dbl>, 3.6 <dbl>, 3.7 <dbl>, 3.8 <dbl>,
#   3.9 <dbl>, 4 <dbl>, 4.1 <dbl>, 4.2 <dbl>, 4.3 <dbl>, 4.4 <dbl>,
#   4.5 <dbl>, 4.6 <dbl>, 4.7 <dbl>, 4.8 <dbl>, 4.9 <dbl>, 5 <dbl>,
#   5.1 <dbl>, 5.2 <dbl>, 5.3 <dbl>, 5.4 <dbl>, 5.5 <dbl>, 5.6 <dbl>,
#   5.7 <dbl>, 5.8 <dbl>, 5.9 <dbl>, 6 <dbl>, 6.1 <dbl>, 6.2 <dbl>,
#   6.3 <dbl>, 6.4 <dbl>, 6.5 <dbl>, 6.6 <dbl>, 6.7 <dbl>, 6.8 <dbl>,
#   6.9 <dbl>, 7 <dbl>, 7.1 <dbl>, 7.2 <dbl>, 7.3 <dbl>, 7.4 <dbl>,
#   7.6 <dbl>, 7.7 <dbl>, 7.9 <dbl>
``````

Answered By – kybazzi

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More