I want to name the rows of a dataframe with a column of a second dataframe. The dataframe to be named has the same number of rows as the dataframe I want to use for naming. Meaning the name of the ith row of the first dataframe shouldhave the name of the ith value of the column of the second dataframe.
I already tried the following:
row.names(df1) <- as.character(df2[,1])
This results in all rows of df1 named after the first value in the vector of dataframe2.
Try this.
#Sample data
df
a b
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
df2
a
1 A
2 B
3 C
4 D
5 E
# Set row names
`rownames<-`(df, df2$a)
# Result
a b
A 1 1
B 2 2
C 3 3
D 4 4
E 5 5
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.