Reputation: 25
I need to convert an array into a data frame in such a way that the row.names are the first column of the data frame. For example I have an array of 4 elements:
big small verybig verysmall
12 3 24 20
converting with as.data.frame gives me (big, small, verybig, verysmall) as row.rames. I want to get a data.frame that looks like this:
row column1 column2
1 big 12
2 small 3
3 verybig 24
4 verysmall 20
where row.names are (1,2,3,4) and (big, small, verybig, verysmall) are in the first data column.
Thanks in advance
Upvotes: 1
Views: 130
Reputation: 6931
What you want is:
vec <- c(big=12, small=3, verybig=24, verysmall=20)
df <- data.frame(col1=names(vec), col2=vec, row.names=NULL)
Upvotes: 1