-3

When using the datatable, the numbers are sorted reading from left to right, instead of right to left. I have it converted to numeric too. Why does it do this? See * numbers It does the same with all columns.

Here's an example: 950 should be the first one, not 96.67

Items   RN.2015 RN.2016 Change.RN
London  30      59      **96.67**
Tokyo   2       21      **950**
Paris   2       21      950
Seoul   2       21      950
New York20      39      95
Orlando 18      35      94.44
Nice    3       31      **933.3**

code in reactive:

library(dplyr)
fileInfo$RN <- as.numeric(as.character(fileInfo$RN))
perChange<-fileInfo %>%
group_by_(input$selCol)  %>%
   summarise( 
      RN.2015 = sum(RN[Year=="2015"]),
      RN.2016 = sum(RN[Year=="2016"])
   )%>%
   mutate(
       Change.RN = delt(RN.2015,RN.2016)
)

function

delt <- function(x,y) {ifelse(is.finite((y-x)/x*100),paste0(formatC((y-x)/x*100),"%"),"")}

data:

Data <- data.frame(
    Items = c("London","Tokyo","Paris","Seoul","New York","Orlando","Nice"),
    RN.2015 = c(30,2,2,2,20,18,3),
    RN.2016 = c(59,21,21,21,39,35,31)
)
C8H10N4O2
  • 18,312
  • 8
  • 98
  • 134
hammies
  • 1,344
  • 2
  • 22
  • 46
  • 2
    [How to make a great R reproducible example?](http://stackoverflow.com/questions/5963269) – zx8754 Sep 16 '16 at 14:17
  • Code and pasted text input data doesn't match. – zx8754 Sep 16 '16 at 14:17
  • thanks for downvoting @zx8754 – hammies Sep 16 '16 at 14:22
  • 4
    First, maybe I did, maybe I didn't. Second, edit your post, I maybe upvote or not. In any case, I am trying to understand **your** problem, but at the same time trying to help **others** who might have similar problem, and not just **you** personally. – zx8754 Sep 16 '16 at 14:25
  • @zx8754 okay well i edited – hammies Sep 16 '16 at 14:26
  • @C8H You might also want to add the corresponding tag in a case like that. Judging by the title, it also needs a DT tag. – Frank Sep 16 '16 at 18:42
  • @Frank I thought so too but it's not a `data.table` question at all – C8H10N4O2 Sep 16 '16 at 18:51
  • @C8H Oh, I meant that you should maybe add the dplyr tag (since you added `library(dplyr)`) and the DT tag. DT is a different package from data.table: http://stackoverflow.com/tags/dt/info The OP might have correctly included it in the title... I'm not sure. – Frank Sep 16 '16 at 18:55
  • Possible duplicate of [How to sort a character vector where elements contain letters and numbers in R?](http://stackoverflow.com/questions/17531403/how-to-sort-a-character-vector-where-elements-contain-letters-and-numbers-in-r) – zx8754 Sep 16 '16 at 18:55

1 Answers1

1

In your function delt, paste0 is returning a character vector, so that column is probably getting converted to character.

The sort behavior above is consistent with the way a numeric value could get sorted if it was incorrectly stored as a character vector. 96.6 > 955 > 94, etc.

Use str(data) and ensure that column Change.RN is actually a numeric. If not, use as.numeric to convert.

C8H10N4O2
  • 18,312
  • 8
  • 98
  • 134