0

I have data that looks like this in df_Filtered (where filtered stands for a df for a specific country):

Year Region    Percentage_of_national_sell
2012 Berlin    84%
2010 Hamburg   101%
2011 Stettin   98%
2012 Stettin   100%
2012 Hamburg   122%
2011 Berlin    111%
2010 Berlin    101%
2010 Stettin   87%
2011 Hamburg   58%

What I want to do is to sort df_Filtered so that I first get it sorted by Region and then by Year (starting with Berlin and then get 2010, 2011, 2012 in that specific order). Both Region and Year should be increasing.

I tried: df_Filtered[ order(df_Filtered$Region, (df_Filtered$Year)) , ] which works when I get the output in text in Rstudio, but it does not seem to change the order of the data frame when I try to remap it:

df_Filtered <- df_Filtered[ order(df_Filtered$Region, (df_Filtered$Year)) , ]

Is there any way to change the order of the rows in the data frame itself? I want to later export it to LaTex to make nice tables, that is why the order is important.

otwtm
  • 1,779
  • 1
  • 16
  • 27
KGB91
  • 630
  • 2
  • 6
  • 24

2 Answers2

3

Using sqldf:

Your sample data:

df=read.table(text="Year Region    Percentage_of_national_sell
          2012 Berlin    84%
          2010 Hamburg   101%
          2011 Stettin   98%
          2012 Stettin   100%
          2012 Hamburg   122%
          2011 Berlin    111%
          2010 Berlin    101%
          2010 Stettin   87%
          2011 Hamburg   58%",header=T)

Code:

library(sqldf)
sqldf("select * from df order by Region, Year")

OR (Base R solution)

df[order(df$Region,df$Year),]

Output:

Year  Region Percentage_of_national_sell
1 2010  Berlin                        101%
2 2011  Berlin                        111%
3 2012  Berlin                         84%
4 2010 Hamburg                        101%
5 2011 Hamburg                         58%
6 2012 Hamburg                        122%
7 2010 Stettin                         87%
8 2011 Stettin                         98%
9 2012 Stettin                        100%
Saurabh Chauhan
  • 3,161
  • 2
  • 19
  • 46
1

The dplyr package works also nice for this: (data is not complete)

library(dplyr)

Region <- c("Ham", "Ste", "Ste", "Ham", "Ber", "Ber")
Year <- c(2012, 2010, 2011, 2012, 2012, 2011)
Percentage <- c(101, 98, 100, 122, 111, 101)
df <- data.frame(Region, Year, Percentage)

df %>% arrange(Region, Year)

Output:

  Year Region Percentage
1 2011    Ber        101
2 2012    Ber        111
3 2012    Ham        101
4 2012    Ham        122
5 2010    Ste         98
6 2011    Ste        100
otwtm
  • 1,779
  • 1
  • 16
  • 27