Home > OS >  How to rearrange a data frame to follow the same order as a seperate data frame?
How to rearrange a data frame to follow the same order as a seperate data frame?

Time:12-07

So I have the following two data frames DataFrame 1 DataFrame 2

What I want is for the top row of DataFrame 2 to follow the same order as the first column in DataFrame 2. For instance, in Df1 the first sample is 'Rk005', how can I change Df2 to follow that same order?

print(Metadata)
      AGE GENDER    VIRUS TNM_STAGE_T TNM_STAGE_T1 PORTAL_VEIN_INVASION VEIN_INVASION ARTERY_INVASION BILE_DUCT_INVASION FIBROSIS
RK001  56   Male     NBNC           2           II                    0             0               0                  0        0
RK002  62   Male      HCV           4           IV                    1             1               0                  0        4
RK003  71   Male      HCV           2           II                    0             0               0                  0        3
RK004  76   Male      HCV           3          III                    0             0               0                  0        4
RK005  66   Male      HBV           2           II                    0             0               0                  0        0
RK006  69   Male      HCV           2           II                    0             0               0                  0        4
RK007  69   Male      HCV           3          III                    0             1               0                  0        2
RK010  46   Male      HBV           3          III                    0             0               0                  0        4
RK012  63   Male      HBV           2           II                    0             0               0                  0        4
> print(Counts)
                 RK326   RK337  RK338   RK305   RK289  RK297  RK285  RK284   RK280  RK277  RK268   RK270  RK264  RK265  RK266   RK259
MPP2              3608     606      0     790   14447      0   1766    404    4937   6368   1414    1366    372   1212   3813     606
TMEM189-UBE2V1  446395 1300584 680589  619439 1041731 130393 907493 343315 1371963 524243 696492 1827581 339501 743908 794153  585123
ELF3           1275154 1157373  73641 1863420  763687 328337 590981 142939 1068558 282583 814017  277128 764833 321378 851416 1470281
SPEG              6104     139    192    1395   11821   3633    985    202     677   4825    697     695    603    595   1264     791
                RK260  RK261  RK262  RK263  RK254  RK256  RK257  RK258   RK237  RK241   RK243  RK244   RK245  RK235   RK236  RK232
MPP2             1017    367    404    202   1210    776   1818   3799   11805   1224    1594    586    2014   1635   27416   2088
TMEM189-UBE2V1 584662 418233 378975 302747 587990 616303 690669 870113 1546547 778494  725563 773820  518712 727809 1636699 999437

Any help is greatly appreciated, thank you!

I have not been able to get anything to work, even remotely.

CodePudding user response:

What you need is the command order, which returns a permutation which can be used to sort its argument. As in, you can try,

x <- sample(1:100, 10, replace=FALSE)
sort(x)  ### The below should give the same result:
x[order(x)]

For the data frame case, see below silly example:

set.seed(7*11*13) 
DF1 <- data.frame(ID=1:10, x=rpois(10,5))
DF2 <- data.frame(y = rpois(10,5)) 
o <- with(DF1, order(x))
DF2.o <- DF2[o,]

CodePudding user response:

I have tried 'colnames(Counts)<-(row.names(Metadata))' which correctly updates the order of the colnames in df2, but does not change rest of the data in the respective columns to match. I need the in those columns to follow.

  • Related