简体   繁体   中英

Convert an array into a dataframe while adding new row.name column

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM