How to preserve the order of a vector in a table in R?

浪子不回头ぞ 提交于 2021-02-04 21:06:50

问题


Pretty simple question, I assume. I am trying to do this for a different type of object (with class 'acf' and type 'list'), but I assume the answer is easily extendable for a vector (class numeric, type 'double'):

 x<-c(4, 5, 6, 1, 2, 10, 15)
 table(x)
 x
 1  2  4  5  6 10 15 
 1  1  1  1  1  1  1 

I would like the output of the table to be in the same order as the vector (4, 5, 6, 1, 2, 10, 15). How can I achieve this?


回答1:


 table(factor(x, levels=unique(x)))


来源:https://stackoverflow.com/questions/25960800/how-to-preserve-the-order-of-a-vector-in-a-table-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!