You can also use data.table
to add the row names as first column then you melt it based on their row names
df<- structure(list(A = c(27143L, 29522L), B = c(18324L, 18875L)), .Names = c("A",
"B"), class = "data.frame", row.names = c("M", "F"))
library(data.table)
library(reshape2)
setDT(df, keep.rownames = TRUE)[]
# rn A B
#1: M 27143 18324
#2: F 29522 18875
(melt(df, id.vars="rn"))
# rn variable value
#1: M A 27143
#2: F A 29522
#3: M B 18324
#4: F B 18875
Or use gather
instead melt
library(tidyr)
gather(df, "rn")
# rn rn value
#1 M A 27143
#2 F A 29522
#3 M B 18324
#4 F B 18875