agustin
agustin

Reputation: 1351

R - dcast from Reshape2 - order by value variable and not variable name

I have following data.frame named countries_tools. It consists of 3 columns (a datetime column (for the last 13 months), a name column (with countries) and a visits column (people visiting the page from those particular countries)):

datetime            name           Visits
2016-07-01 00:00:00 China          5237
2016-07-01 00:00:00 Germany        1434
2016-07-01 00:00:00 United States  1530
2016-07-01 00:00:00 India          696
2016-07-01 00:00:00 Japan          569
...
2017-07-01 00:00:00 China          4484
2017-07-01 00:00:00 Germany        1593
2017-07-01 00:00:00 United States  1438
2017-07-01 00:00:00 India          1204
2017-07-01 00:00:00 Japan          538

Please take note that I removed the other 11 months in between. Also take note, that name is always a list of the same 5 countries, which correspond with the five countries with greater number of visits in the last analysed month (July 2017 in this case).

At the end of this message there is a dput with my data.

For better understanding the data and development of visits across months, I do a dcast from my data.frame:

countries_tools <- dcast(countries_tools, datetime ~ name, value.var="Visits")

However, the resulting dataframe orders the columns by country name (alphabetically):

> names(countries_tools)
[1] "datetime"      "China"         "Germany"       "India"         "Japan"         "United States"

Order is alphabetical, not by variable value

I expect, however, that the order is done by the value variable (visits) and therefore the optimal order should be:

datetime, China, Germany, United States, India, Japan

Can it be done (at best if it is not needed an extra step)? Using other functions is also a possibility.

Data

dput(countries_tools)
structure(list(datetime = structure(c(1467320400, 1467320400, 
1467320400, 1467320400, 1467320400, 1469998800, 1469998800, 1469998800, 
1469998800, 1469998800, 1472677200, 1472677200, 1472677200, 1472677200, 
1472677200, 1475269200, 1475269200, 1475269200, 1475269200, 1475269200, 
1477951200, 1477951200, 1477951200, 1477951200, 1477951200, 1480543200, 
1480543200, 1480543200, 1480543200, 1480543200, 1483221600, 1483221600, 
1483221600, 1483221600, 1483221600, 1485900000, 1485900000, 1485900000, 
1485900000, 1485900000, 1488319200, 1488319200, 1488319200, 1488319200, 
1488319200, 1490994000, 1490994000, 1490994000, 1490994000, 1490994000, 
1493586000, 1493586000, 1493586000, 1493586000, 1493586000, 1496264400, 
1496264400, 1496264400, 1496264400, 1496264400, 1498856400, 1498856400, 
1498856400, 1498856400, 1498856400), class = c("POSIXct", "POSIXt"
), tzone = "Europe/Moscow"), name = c("China", "Germany", "United States", 
"India", "Japan", "China", "Germany", "United States", "India", 
"Japan", "China", "Germany", "United States", "India", "Japan", 
"China", "Germany", "United States", "India", "Japan", "China", 
"Germany", "United States", "India", "Japan", "China", "Germany", 
"United States", "India", "Japan", "China", "Germany", "United States", 
"India", "Japan", "China", "Germany", "United States", "India", 
"Japan", "China", "Germany", "United States", "India", "Japan", 
"China", "Germany", "United States", "India", "Japan", "China", 
"Germany", "United States", "India", "Japan", "China", "Germany", 
"United States", "India", "Japan", "China", "Germany", "United States", 
"India", "Japan"), Visits = c(5237, 1434, 1530, 696, 569, 4422, 
1508, 1971, 672, 461, 3993, 1521, 1901, 2027, 517, 3656, 1764, 
1716, 993, 509, 5483, 3117, 2762, 1298, 594, 5548, 2804, 2365, 
1222, 551, 3747, 3083, 1917, 999, 496, 3903, 2136, 1751, 1229, 
611, 5638, 2721, 2074, 1569, 533, 4326, 1618, 1511, 1254, 458, 
4364, 2021, 1690, 1162, 462, 4462, 1572, 1517, 1068, 574, 4484, 
1593, 1438, 1204, 538)), .Names = c("datetime", "name", "Visits"
), row.names = c(NA, -65L), class = "data.frame")

Upvotes: 1

Views: 668

Answers (1)

Davide Passaretti
Davide Passaretti

Reputation: 2761

You can convert "name" to an ordered factor stating the order you want for the levels:

countries_tools$name <- ordered(countries_tools$name, levels = unique(countries_tools$name))

Now it works:

dcast(countries_tools, datetime ~ name, value.var="Visits")

Upvotes: 2

Related Questions