Order rows using a dataframe

Hi everyone, I'm freshly using R and I'm having some problem to order my code

I have a data frame (df1) of some economic data like this:

account  1st quarter  2nd quarter  3rd quarter 4th quarter       sum
<list>  <int> <list> <int> <list> <int> <list> <int> <list> <int> <list>
account3     1            2            3            1             7
account2     2            3            1            2             8
account1     1            2            1            3             7 
   .         .            .            .            .             .
   .         .            .            .            .             .
   .         .            .            .            .             .

And another dataframe (df2) having the same numbers of rows of (df1) as:

account  
<list>
account1 
account2 
account3   
   .      
   .        
   .    

I'm trying to get something like this:

account  1st quarter  2nd quarter  3rd quarter 4th quarter       sum
<list>  <int> <list> <int> <list> <int> <list> <int> <list> <int> <list>
account1     1            2            1            3             7
account2     2            3            1            2             8
account3     1            2            3            1             7 
   .         .            .            .            .             .
   .         .            .            .            .             .
   .         .            .            .            .             .

How I can reorder the dataframe (df1) according to dataframe (df2)?

I just tried some codes that I found like:

library(data.table)

setroworder <- function(x, neworder) {
  .Call(data.table:::Creorder, x, as.integer(neworder), PACKAGE = "data.table")
  invisible(x)
}

setroworder(x=df1$account, neworder=df2$account)

and:

df3 <- as.data.frame(df1)

df3[match(rownames(df1), rownames(df2)),]

and:

df3 <- as.data.frame(df1)
df3[order(match(df1$account,df2$account)]

and:

df3 <- as.data.frame(df1)
df3[ordered(df1$account,df2$account)]

and:

order(rownames(df1$account,df2$account))

So, anyone knows how to solve this?

dplyr::left_join(df2, df1, by = 'account')

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.