Hey guys I'm pretty new to R so apologies if this is a dumb question. I have a data frame that looks like this:
Gelatin Geltrex Combo Control
1 0.761 0.732 0.557 0.723
2 0.704 0.712 0.764 0.540
3 0.580 0.435 0.351 0.537
I want to end up with 2 columns; one with the numbers in the row and another column with the current column names as identifiers for the numbers?
If we need row/column index we can use row
and col
to get the row/column index and create a new dataset with data.frame
.
df2 <- data.frame(Row= c(row(df1)),
Col=names(df1)[col(df1)], stringsAsFactors=FALSE)
Or another option is melt
from library(reshape2)
. We convert the 'data.frame' to 'matrix' and melt
to get the row/column names as the first two columns.
library(reshape2)
melt(as.matrix(df1))[-3]
EDIT:
If we need the value
and column names as the two columns, just melt
or use stack
as @thelatemail mentioned
melt(df1)
Or stack
from base R
stack(df1)