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 @Mark6 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.