r - Converting column with pipe delimited data into dummy variables -


i'm interested in taking column of data.frame values in column pipe delimited , creating dummy variables pipe-delimited values.

for example:

let's start with

df = data.frame(a = c("ben|chris|jim", "ben|greg|jim|", "jim|steve|ben"))  > df               1 ben|chris|jim 2 ben|greg|jim 3 jim|steve|ben 

i'm interested in ending with:

df2 = data.frame(ben = c(1, 1, 1), chris = c(1, 0, 0), jim = c(1, 1, 1), greg = c(0, 1, 0),                   steve = c(0, 0, 1)) > df2   ben chris jim greg steve 1   1     1   1    0     0 2   1     0   1    1     0 3   1     0   1    0     1 

i don't know in advance how many potential values there within field. in example above, variable "a" can include 1 value or 10 values. assume reasonable number (i.e., < 100 possible values).

any ways this?

another way using csplit_e splitstackshape package.

splitting dataframe column a , fill 0 , drop original column.

library(splitstackshape) csplit_e(df, "a", "|", type = "character", fill = 0, drop = t)  #   a_ben a_chris a_greg a_jim a_steve #1     1       1      0     1       0 #2     1       0      1     1       0 #3     1       0      0     1       1 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -