We may use split
in base R
lst1 <- split(df1$col1, df1$col1)
mx <- max(lengths(lst1))
data.frame(lapply(lst1, `length<-`, mx))
Or if we want to create two columns
library(dplyr)
library(tidyr)
library(data.table)
df1 %>%
mutate(rn = rowid(col1),
colnm = case_when(col1 == 0 ~ "col1", TRUE ~ "col2")) %>%
pivot_wider(names_from = colnm, values_from = col1) %>%
select(-rn)
-output
# A tibble: 8 × 2
col1 col2
<dbl> <dbl>
1 0 1
2 0 1
3 0 1
4 0 1
5 0 1
6 0 1
7 0 1
8 0 NA
data
df1 <- structure(list(col1 = c(0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1,
1, 1)), class = "data.frame", row.names = c(NA, -15L))