Reputation: 195
I'm ordering the rows of a data frame based on my row names and an external vector. The vector can be shorter than the number of rows.
I've tried different combinations of match and order, but they don't want to take rownames(df) as an argument, or it creates a vector of factors instead
df <- data.frame(info = c("cluster1","cluster7", "cluster1", "cluster3"),row.names = c("gacg","cggt","cgat", "ccat"))
vector <- c("gacg","cgat","cggt")
info
gacg cluster1
cggt cluster7
cgat cluster1
ccar cluster3
I tried variants of this solution:
df2<-df[order(match(df$rownames,vector)),] #produces some type of vector of factors
I would like a data frame that is ordered with the rownames in the same order as the vector, and mismatching rows where the vector data frame is shorter than the vector should be placed at the end.
info
gacg cluster1
cgat cluster1
cggt cluster7
ccar cluster3
Upvotes: 2
Views: 2540
Reputation: 5491
You can use :
df[order(match(rownames(df), vector)), , drop = FALSE]
Upvotes: 6