r - Convert Character Matrix to TRUE/FALSE Matrix based on column names -


i have data frame in following format

    1 2 b c 1   b 0 0 0 2   b   0 0 0 3   c   0 0 0 

i want fill columns through c true/false says whether column name in columns 1 or 2

    1 2 b c 1   b 1 1 0 2   b   0 1 0 3   c   0 0 1 

i have dataset of 530,000 records, 4 description columns, , 95 output columns loop not work. have tried code in following format, time consuming:

> for(i in 3:5) {    >   for(j in 1:3) { >     for(k in 1:2){ >       if(df[j,k]==colnames(df)[i]) df[j, i]=1 >     }    >   }  > } 

is there easier, more efficient way achieve same output?

thanks in advance!

one option mtabulate qdaptools

library(qdaptools) df1[-(1:2)] <- mtabulate(as.data.frame(t(df1[1:2])))[-3] df1 #  1 2 b c #1 b 1 1 0 #2 b   0 1 0 #3 c   0 0 1 

or melt dataset after converting matrix, use table frequencies, , assign output columns numeric.

library(reshape2) df1[-(1:2)] <- table(melt(as.matrix(df1[1:2]))[-2])[,-1] 

or can 'paste' first 2 columns , use csplit_e binary format.

library(splitstackshape) cbind(df1[1:2], csplit_e(as.data.table(do.call(paste, df1[1:2])),                    'v1', ' ', type='character', fill=0, drop=true)) 

data

df1 <- structure(list(`1` = c("a", "b", "c"), `2` = c("b", "", ""),  = c(0l, 0l, 0l), b = c(0l, 0l, 0l), c = c(0l, 0l, 0l)), .names = c("1",  "2", "a", "b", "c"), class = "data.frame", row.names = c("1",  "2", "3")) 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -