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

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -