Reputation: 1619
I have a data frame that looks like this:
> testdata
topic1 topic2 topic3 topic4 topic5
church 0.011 0.003 0.001 0.001 0.012
of 0.094 0.085 0.098 0.063 0.051
the 0.143 0.115 0.159 0.083 0.097
appearance 0.000 0.000 0.002 0.005 0.040
restrain 0.000 0.000 0.000 0.000 0.000
What I need to do is create a new data frame that is also 5 rows by 5 columns where each column is the ordered row names of this data frame. In other words, I need to order the data frame by each column in descending order, then print the row names on top of that column, essentially to get the ranked words in order. For this example, the data frame I need would be
> testdata_word_ranks
topic1 topic2 topic3 topic4 topic5
church the the the the the
of of of of of of
the church church appearance appearance appearance
appearance appearance appearance church church church
restrain restrain restrain restrain restrain restrain
Here is my failed attempt at assigning the columns of testdata_word_ranks
above to a new data frame:
for(i in 1:nrow(testdata)){
minidf = data.frame(rownames(testdata), testdata[,i])
assign(paste0('testdata_word_ranks$topic', i),
as.vector(minidf[order(minidf[,2], decreasing = TRUE),]$rownames.testdata))
}
Just for your information, this data is coming from a topic model on a particular corpus.
Upvotes: 2
Views: 52
Reputation: 44340
You could index the row names by the order of each column:
matrix(row.names(test.data)[apply(-test.data, 2, order)], nrow(test.data))
# [,1] [,2] [,3] [,4] [,5]
# [1,] "the" "the" "the" "the" "the"
# [2,] "of" "of" "of" "of" "of"
# [3,] "church" "church" "appearance" "appearance" "appearance"
# [4,] "appearance" "appearance" "church" "church" "church"
# [5,] "restrain" "restrain" "restrain" "restrain" "restrain"
Upvotes: 3