简体   繁体   中英

Changing variable from factor to numeric changes the order

I am working on a text analysis project in R where I'm attempting to identify words that tend to be used more by one group compared to another (based on bayesian probability). I now have a list with words and word loadings (factors). When I convert the word loadings from factor to numeric, the order changes. Why is this and how do I solve it?

在此处输入图片说明

Then running the following code:

words$top_words <- as.numeric(words$top_words)

在此处输入图片说明

Thank you very much for your help!

words$top_words is factor, hence when converted to numeric they are changed to underlying numbers. To safely convert factors to numbers we can do

words$top_words <- as.numeric(as.character(words$top_words)) 

一个选择也是利用levels会更快

words$top_words <- as.numeric(levels(words$top_words)[words$top_words])

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