R covert values in rows with string of multiple values to columns in a dataframe -


i have dataframe {each row in b string values joined $ symbol}:

a b 1$2$3 b 2$4$5 c 3$2$5 

now want this{i want create columns value present in row(of column b) or not.}:

a b     1 2 3 4 5 1$2$3 1 1 1 0 0 b 2$4$5 0 1 0 1 1 c 3$5   0 0 1 0 1 

i want without using loops in r. please me

thanks in advance

here's attempt. first, unique values across b column , combine table factor while specifying these levels splits of b column (edited after comments @akrun)

temp <- strsplit(as.character(df$b), "\\$") # save split column  lvls <- unique(unlist(temp)) # unique values df[lvls] <- do.call(rbind, lapply(temp, function(x) table(factor(x, levels = lvls)))) df  #       b 1 2 3 4 5 # 1 1$2$3 1 1 1 0 0 # 2 b 2$4$5 0 1 0 1 1 # 3 c 3$2$5 0 1 1 0 1 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -