I am an R (and coding novice) and I am looking for a way to reconfigure Table A show below into Table B.
Table A:
type x1 x2 x3
A 4 6 9
A 7 4 1
A 9 6 2
B 1 3 8
B 2 7 9
I am looking for code that would convert to the following
Table B:
type x1 x2 x3 x1' x2' x3' x1'' x2'' x3''
A 4 6 9 7 4 1 9 6 2
B 1 3 8 2 7 9
The real Table A is over 150000 rows and 36 Columns. With 2100 unique "type" values.
Thanks for the help.
-Shawn
Variation on @Hack-R's answer:
And a dplyr version for fun:
To me, this solution seems pretty straightforward
If the order of the output above is not satisfactory, you can rearrange:
To add a column for type instead of using row names, one method would be:
Take a try and the solution is not such concise, just give you a hint.I think lots of things can be improved.
But finally we have to introduce NAs in here :(
A bit late to the party, but this can also be done quite easily with the
dcast
function of thedata.table
package because you can use multiplevalue.var
's in it:which gives:
You can also do this in base R with: