How can I turn the following matrix (or table/data frame) with row names & column names,
A B
M 27143 18324
F 29522 18875
into something like
27143 M A
18324 M B
29522 F A
18875 F B
so that I can do some analysis in R?
You can use the reshape2
package and melt
the data.
temp = read.table(header=TRUE, text=" A B
M 27143 18324
F 29522 18875")
library(reshape2)
temp$id = rownames(temp)
melt(temp)
# Using id as id variables
# id variable value
# 1 M A 27143
# 2 F A 29522
# 3 M B 18324
# 4 F B 18875
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With