0

I am trying to split a binary column into two separate columns based on the 0,1

     account  
row1 0
row2 1
row3 0
row4 1
row5 0
row6 0

     account_0 | account_1 
row1 0         |  
row2           |  1
row3 0         |
row4           |  1
row5 0         |
row6 0         |

I tried using the separate function. But it is expecting a delimiter to separate the columns

test=separate(df, col = account, into = c("account_0","account
-1"), sep = ?) [not sure what to pass for the argument "sep"]

View(test)

Can someone please help?

Dale K
  • 25,246
  • 15
  • 42
  • 71
rgirl
  • 1
  • 4
  • 2
    Possible duplicate of [How to reshape data from long to wide format](https://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format) – Shree Jun 13 '19 at 23:33
  • 2
    What goes into the vacated cells? `NA`? – r2evans Jun 13 '19 at 23:37
  • This really depends on what you are doing. If you want to have a set of dummy variables, then you don't need to split. My suspicion is that you should keep a single variable. In R, look into factor variables for modeling and splitting up observations for figures. – lmo Jun 13 '19 at 23:43

2 Answers2

1
reshape(transform(df,time=account),dir="wide",idvar = "id")
    id account.0 account.1
1 row1         0        NA
2 row2        NA         1
3 row3         0        NA
4 row4        NA         1
5 row5         0        NA
6 row6         0        NA

read.csv(text = sub("(,1)",',\\1',do.call(paste,c(sep=',',df))),fill=T,h=F)
    V1 V2 V3
1 row1  0 NA
2 row2 NA  1
3 row3  0 NA
4 row4 NA  1
5 row5  0 NA
6 row6  0 NA
Onyambu
  • 67,392
  • 3
  • 24
  • 53
  • 1
    My suspicion is that the user in not aware of formulas and factors in R. That being said, please consider using TRUE and FALSE rather than T and F in your answers as it invites potentially unintended results. Consider that `T <- FALSE` is totally fine with R. – lmo Jun 13 '19 at 23:49
0

Assuming the vacated cells are spaces, here 0's and 1's will also be characters. Can be done using dplyr. You can use select to choose whatever columns you want. TestData <- fread("RowNumber account
row1 0 row2 1 row3 0 row4 1 row5 0 row6 0")

TestData %>% mutate(account_0  = ifelse(account == 0, "0"," ")) %>% mutate(account_1  = ifelse(account == 1, "1"," "))

   RowNumber account account_0 account_1
1:      row1       0         0          
2:      row2       1                   1
3:      row3       0         0          
4:      row4       1                   1
5:      row5       0         0          
6:      row6       0         0    
Jason Mathews
  • 765
  • 3
  • 13