Reputation: 29
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.
Upvotes: 0
Views: 331
Reputation: 1421
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
Upvotes: 0