Sorting with dates


#1

How can a table be created with sorted data(sorted data includes dates)
I have tried below code , but didnt worked.
tab1<-sort(data1$monthyear_speci) >table(data1$Name,data1$monthyear_speci)


#2

Check out the tidyverse's dplyr package, and the arrange function. Your pipe might look something like;

df <- data1 %>%
   arrange(monthyear_speci)

I get a sense that you might be new to R? If you are, check out this dplyr vignette, it works up to arrange and other useful functions for data wrangling.

RStudio also put together a little playlist about data-wrangling you might find useful: Data Wrangling with R and the Tidyverse - 4 videos


#4

If you are working with dates (and times) in R, I'd recommend the lubridate package. I often find it a massive help.


#5

the advantage to lubridate is that sorting dates with arrange will just work. Otherwise, you need to make sure your dates are in something like yyyy-mm-dd order (perhaps yyyy-mm in your case) as text so that they will sort into the right order. Examples:

library(tidyverse)
d=tribble(
  ~y, ~date,
  10, "2010-07",
  11, "2009-06",
  12, "2010-01"
)
d %>% arrange(date)

or, turning them into actual dates,

library(lubridate)
d=tribble(
  ~y, ~textdate,
  10, "July 2, 2010",
  11, "June 14, 2009",
  12, "January 7, 2010"
)
d %>% mutate(date=mdy(textdate)) %>%
  arrange(date)

#6

In the good old days you could simply use order.

library(tidyverse)
library(lubridate)

df <- tribble(
  ~y, ~date,
  10, "2010-07-02",
  11, "2009-06-14",
  12, "2010-01-07"
)
df %>% mutate(date = ymd(date)) -> df
df[order(df$date),]
#> # A tibble: 3 x 2
#>       y date      
#>   <dbl> <date>    
#> 1   11. 2009-06-14
#> 2   12. 2010-01-07
#> 3   10. 2010-07-02

#7

It looks like maybe you're trying to make a contingency table? If your dates are recognized as dates by R, then they should automatically show up in chronological order as column names for your table.

set.seed(20)

tab <- table(
  sample(rownames(mtcars)[1:5], 100, replace = TRUE),
  sample(seq.Date(as.Date("2000-01-01"), as.Date("2000-03-01"), by = "month"), 100, replace = TRUE)
)

tab
#>                    
#>                     2000-01-01 2000-02-01 2000-03-01
#>   Datsun 710                 6          9         11
#>   Hornet 4 Drive             5          2          6
#>   Hornet Sportabout          4          8          8
#>   Mazda RX4                  9          7          9
#>   Mazda RX4 Wag              4          7          5

If the columns of your table aren't in chronological order, then it's probably because data1$monthyear_speci is not understood as dates by R. For instance, if R sees it as character data, then the values will be sorted alphabetically. As @mark recommended, lubridate is very helpful for getting your dates to be understood as dates by R.

If you are trying to sort the contents of a contingency table, you can't use dplyr::arrange without first converting your table to a data frame (and then converting it back when you're done).
Old-fashioned order can sort contingency tables, though the syntax isn't the easiest to read.

# Sort the table by the third column, decreasing
tab[order(tab[, 3], decreasing = TRUE), ]
#>                    
#>                     2000-01-01 2000-02-01 2000-03-01
#>   Datsun 710                 6          9         11
#>   Mazda RX4                  9          7          9
#>   Hornet Sportabout          4          8          8
#>   Hornet 4 Drive             5          2          6
#>   Mazda RX4 Wag              4          7          5

By the way, assuming that your code was meant to read as:

tab1 <- sort(data1$monthyear_speci) %>% table(data1$Name, data1$monthyear_speci)

one problem is that the pipe operator (%>%) passes the previous step of the pipe into the next function as its first argument. So what you wrote was equivalent to:

tab1 <- table(sort(data1$monthyear_speci), data1$Name, data1$monthyear_speci)

which makes a pretty nonsensical 3d table.


#8

thank you , your mail helped me lot.

Regards
Shalini


#9

thank you Wilson , your mail helped me lot.

Regards
Shalini


#10

thank you Ken , your mail helped me lot.

Regards
Shalini