Pradeep Pradeep - 1 month ago 16
R Question

r tm extract doc ids after sort operation

I have mindfreeze around this issue. I am extracting most frequent words from a

tm::dtm
, like so:

> s1<-sort(rowSums(as.matrix(dtm10[,])), decreasing=TRUE)


I get:

290 429 318 125 128 425 431 153 52 385 144 491 126 423 111 130 492 163 176 391
916 875 860 851 844 823 822 766 759 743 701 700 686 673 670 669 663 658 652 623`


But the doc ids and
rowSums
are in a tuple.

> s1[2]
429
875


where 429 is doc id and rowsum is 875. I have no further use of the
rowSums
, how do I get a list of the sorted doc ids? I am looking for a vector output like:

290 429 318 125 128 425 431 153 52 385 144 491 126 423 111 130 492 163 176 391


Many thanks.

Answer

s1 is a named vector with names as 290, 429, 318 etc and values like 916,875, 860 and so on.

To extract only the names of s1 , we can use,

names(s1)

which would give :

#290 429 318 125 128 425 431 153  52 385 144 491 126 423 111 130 492 163 176 391