I have the solution to the following problem, but it is quite ugly and it can be done in the least way. It will be great to get your help.
My input looks like this:
C1 C2 ABDDCAD and my output should look like this: < / P>
ABCD 1 0 0 0 1 0 1 0 0 1 1 1 0 0 1 My current solution is:
< Code> index and lieutenant; - Unique (Initial 1, 2)) output & lt; - For matrix (0, nrows = dim (input), ncol = length (index)) (i in 1: diminish) {output [i, input [i,] $ c1] Of course, the 4-5 line is actually fine - but even the beginning of R is not the right to use the for loop as its beginning Apart from my actual data there are more than two columns, so it does not look good at the end. How will I do this cleverly? Cheers
Here's the way:
Library plyr all.levels & lt; - Sort (level (list)) adply (input, 1, function (x) table (factor (list (list (x), all.levels)) # c1c2a abcd # 1 ab1 1 0 0 # 2 BD 0 1 0 1 # 3 DC 0 0 1 1 # 4 AD1 0 0 1
Comments
Post a Comment