Ordering factors in data table using DT package

后端 未结 1 1171
无人共我
无人共我 2021-01-14 06:47

I have data that I want to display in 5% increments, such as 5-10%, 10-15%, etc. To do this, I have a data frame that stores them as a factor, with the levels being the mid

相关标签:
1条回答
  • 2021-01-14 07:32

    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))))
    
    0 讨论(0)
提交回复
热议问题