-1

I have four outputs (W, X, Y, and Z) in the form of data frame with common column (a, b, c) but having other different other columns. How can I merge them considering the order of each value of each dataframe. I have used the following syntax, and I could able to merge them but it hasn't given me in ascending order of each value, rather it gives me in random order.

Output <- Reduce(function(...) merge(..., by = c("a", "b", "c"), all=TRUE), list (W,X,Y, Z), accumulate = FALSE)

This produces the merged data but in unorderly manner and how could I adjust it to give in ascending order of the values in each dataframe? How can I get in order of a like 1, 3, 5, 9,..... (ascending values) with the corresponding value of all other parameters.

Sample data:

    a  b c   Int     R     Sl   P   D
    1  1 1   0.32 
    1  1 2   0.52 
    ........................
    3  1 1   1.56   0.23   0.36 0.455  0.52
    
    -----------------------------------
    9  1  1   0.526 0.46  0.56  0.566  0.322

   .........................................
    5 1  1      0.89  0.36  0.56  0.666   0.5222
   ............................................
  • 2
    Can you share some example data with an expected output? – Ronak Shah Feb 08 '21 at 03:23
  • @Ronak Shah, thanks! I just want to keep the order of common columns (a, b, c) with their corresponding values of other columns in ascending order or only keeping the order of (a) column if it is not possible to keep the order of column b and c. – Stackoverflowuser Feb 08 '21 at 04:38

1 Answers1

0

If the dataframe above represents what you get when you run your code, then you should be able to do the following to get ascending order of column 'a':

# Load libraries
library('tidyverse')

# Sort dataframe in ascending order by column 'a'
OutputSorted <- Output %>% arrange(a)
C Jeruzal
  • 538
  • 2
  • 10
  • @C Jeruzal, can you recommend me how to merge the dataframes using common columns (a, b, c and Int), I don't know why I am getting error of "Error in dimnames(x) <- dn : length of 'dimnames' [2] not equal to array extent", thanks! – Stackoverflowuser Feb 10 '21 at 22:06
  • @Stackoverflowuser, Sure, I like to use `dplyr::left_join`, `dplyr::full_join`, etc. Check out the help vignettes on joining or merging data by running `?dplyr::left_join`. If you want something more specific, share a reprex of the two dataframes you're trying to merge. Here was a useful example on StackOverflow (https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right?rq=1) – C Jeruzal Feb 11 '21 at 01:43