I have a data frame in R that I want to transpose into a different format, please see the example below: Can I use the transpose function in R?
Input data frame:
Samples A1 A2 A3 B1 B2 B3
Sample1 123 123 321 32 321 32132
Sample2 12321 32321 2321 2313 3213 3123
Sample3 454 54 543 543 43 435
Desired Output:
Samples 1 2 3
Sample1 A 123 123 321
Sample1 B 32 321 32132
Sample2 A 12321 32321 2321
Sample2 B 2313 3213 3123
Sample3 A 454 54 543
Sample3 B 543 43 435
I think you are looking for the package
reshape2
. It has two functions,melt
anddcast
that can help you accomplish what you want.If
d
is the datasetUsing
dplyr
I think you can use
t()
function to transpose the matrix.To give some props to base R, look at the
reshape()
function. Assuming yourdata.frame
is called "mydf", try: