How to change the order of a data frame in R

I endlessly searched for it and for some reason did not solve this simple problem.

I have a dataframe called Prices, which has 4 columns, one of which is a list of historical dates - the other 3 are price lists for products.

1 10/10/2016 53.14 50.366 51.87 2 07/10/2016 51.93 49.207 50.38 3 06/10/2016 52.51 49.655 50.98 4 05/10/2016 51.86 49.076 50.38 5 04/10/2016 50.87 48.186 49.3 6 03/10/2016 50.89 48.075 49.4 7 30/09/2016 50.19 47.384 48.82 8 29/09/2016 49.81 46.924 48.4 9 28/09/2016 49.24 46.062 47.65 10 27/09/2016 46.52 43.599 45.24 

The list contains 252 prices. How can I save my output with the last date at the bottom of the list and the corresponding prices indicated at the latest prices at the bottom of the list?

+22
source share
5 answers

If you just want to reorder the rows in the data frame, you can do the following:

 df<- df[seq(dim(df)[1],1),] 
+12
source

Just for the sake of completeness. Actually there is no need to call seq . You can simply use : -R-logic:

 ### Create some sample data n=252 sampledata<-data.frame(a=sample(letters,n,replace=TRUE),b=rnorm(n,1,0.7), c=rnorm(n,1,0.6),d=runif(n)) ### Compare some different ways to reorder the dataframe myfun1<-function(df=sampledata){df<-df[seq(nrow(df),1),]} myfun2<-function(df=sampledata){df<-df[seq(dim(df)[1],1),]} myfun3<-function(df=sampledata){df<-df[dim(df)[1]:1,]} myfun4<-function(df=sampledata){df<-df[nrow(df):1,]} ### Microbenchmark the functions microbenchmark::microbenchmark(myfun1(),myfun2(),myfun3(),myfun4(),times=1000L) Unit: microseconds expr min lq mean median uq max neval myfun1() 63.994 67.686 117.61797 71.3780 87.3765 5818.494 1000 myfun2() 63.173 67.686 99.29120 70.9680 87.7865 2299.258 1000 myfun3() 56.610 60.302 92.18913 62.7635 76.9155 3241.522 1000 myfun4() 56.610 60.302 99.52666 63.1740 77.5310 4440.582 1000 

The fastest way in my test here was to use df<-df[dim(df)[1]:1,] . However, using nrow instead of dim is a bit slower. Make it a matter of personal preference.

Using seq here definitely slows down the process.

UPDATE September 2018:

In terms of speed, there are few reasons to use dplyr . Perhaps for 90% of users the basic R functionality should be enough. The remaining 10% should use dplyr to query the database or translate code into another language.

 ## hmhensen function dplyr_fun<-function(df=sampledata){df %>% arrange(rev(rownames(.)))} microbenchmark::microbenchmark(myfun3(),myfun4(),dplyr_fun(),times=1000L) Unit: microseconds expr min lq mean median uq max neval myfun3() 55.8 69.75 132.8178 103.85 139.95 8949.3 1000 myfun4() 55.9 68.40 115.6418 100.05 135.00 2409.1 1000 dplyr_fun() 1364.8 1541.15 2173.0717 1786.10 2757.80 8434.8 1000 
+10
source

Another tidyverse solution and I think the simplest is:

df %>% map_df(rev)

or using just purrr::map_df we can do map_df(df, rev) .

+7
source

Here's a dplyr ( tidyverse ) solution to the OP question on how to reorder strings.

Assuming the data frame is called df , then we can do:

 df %>% arrange(rev(rownames(.))) 

Explanation: "." the placeholder accepts the entered data frame as a frame. Then rownames(df) become the index vector, 1:nrow(df) . rev arrange and arrange reorders df accordingly.

Without a pipe, the following does the same:

 arrange(df, rev(rownames(df))) 

If the OP would first convert its dates to Date or POSIX format, as described in the comments, then of course it could just use df %>% arrange(Date) .

But the first method is what answers the OP question.

+3
source

If you want to stick to the R base, you can also use lapply() .

 do.call(cbind, lapply(df, rev)) 
0
source

All Articles