Ordering factors in data table using DT package

我的未来我决定 提交于 2019-12-06 02:42:09

You could use a hidden column with the numeric value of the factor and sort the factors according to that hidden column:

library('DT')
value <- factor(c(7.5, 12.5, 7.5, 17.5),
                levels = c(7.5, 12.5, 17.5),
                labels = c('5-10%', '10-15%', '15-20%'))

example <- data.frame(name = c('A', 'B', 'C', 'D'),
                      value = value,
                      levels=as.numeric(value))

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