Home > Blockchain >  How to separate an integer column?
How to separate an integer column?

Time:01-14

I have a dataset that has a column which consists of 0 and 1s. Is there a way that I can separate that column into two columns so that one column consists of the 0s and the other 1s?

I tried to used the separate function from tidyverse however, it hasn't worked yet.

CodePudding user response:

You can use gsub twice:

library(dplyr)
df %>% 
  mutate(zeros = gsub("1", "", int),
         ones = gsub("0", "", int))

#          int  zeros  ones
#1   011001010  00000  1111
#2  1001001110  00000 11111
#3 10101011000 000000 11111

Or with strrep str_count:

library(stringr)
df %>% 
  mutate(zeros = strrep("0", str_count(int, "0")),
         ones = strrep("1", str_count(int, "1")))

data:

df <- data.frame(int = c("011001010", "1001001110", "10101011000"))
#          int
#1   011001010
#2  1001001110
#3 10101011000

CodePudding user response:

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))

CodePudding user response:

A simple ifelse statement could also be used to do this. -

df <- as.data.frame(c(0,0,1,0,1,1,0,0,0,0,0,1,0,0,0,0,1,1,0))

names(df)<-"col1"

df$col_0 <- ifelse(df$col1 == 0, 0, NA)
df$col_1 <- ifelse(df$col1 == 1, 1, NA)

Returns the following result:

> df
   col1 col_0 col_1
1     0     0    NA
2     0     0    NA
3     1    NA     1
4     0     0    NA
5     1    NA     1
6     1    NA     1
7     0     0    NA
8     0     0    NA
9     0     0    NA
10    0     0    NA
11    0     0    NA
12    1    NA     1
13    0     0    NA
14    0     0    NA
15    0     0    NA
16    0     0    NA
17    1    NA     1
18    1    NA     1
19    0     0    NA
  •  Tags:  
  • r
  • Related