0

I am using the pander function to make my tables in R markdown, but having some troubles with cells being ordered in an illogical way. here is a screenshot of my r Markdown As you can see, it is ordering based on the first digit as opposed to the value of the whole number. here is my script:

income.frequency.table <- xtabs(~income, data=iceland)
pander(income.frequency.table)

any help would be really appreciated. I am very new to R and coding in general so apologies if I've missed something very obvious! Thanks in advance.

Emily
  • 29
  • 1
  • 7

1 Answers1

0

The labels are characters that just happen to be numbers, so the sort is lexical, not numeric. This is why e.g. 1900000 precedes 400000; because 1 precedes 4.

A general solution to this is to make the text an ordered factor. The intended order will be preserved.

x <- c("4", "10")
sort(x)  # unexpected
# [1] "10"  "4"

y <- ordered(x, levels = c("4", "10"))
sort(y)  # as intended
# [1] 4  10
# Levels: 4 < 10
effel
  • 1,421
  • 1
  • 9
  • 17
  • thanks very much for your help. I reordered the text using the reorder function in the g.data package, and now it's working fine. – Emily Jun 09 '16 at 01:50