Reputation: 147
Thanks to stack I am able to sort this dataframe, first according to "status", then by "ID", which looks like:
>pheno
ID status
1 patient19 0
2 patient21 0
3 patient7 1
4 patient10 1
(Code to make the pheno df):
ID = c("patient19", "patient21", "patient7", "patient10")
pheno = as.data.frame(ID)
pheno$status = c("0", "0", "1", "1")
row.names(phenodf) = pheno$ID
But now I have a second data frame in which the IDs are now the column titles, and these need be sorted so that it matches the order they're in with the pheno df. How can I do this?
>genes
gene patient7 patient21 patient19 patient10
ABC 1.5 2.3 3.3 4.4
A2B 2.5 1.3 3.1 2.3
DE5 3.5 3.3 3.4 1.4
ZXY 4.5 4.3 3.6 5.1
(Code to make the genes df):
patient7 = c(1.5, 2.5, 3.5, 4.5)
genes = as.data.frame(patient7)
genes$patient21 = c(2.3, 1.3, 3.3, 4.3)
genes$patient19 = c(3.3, 3.1, 3.4, 3.6)
genes$patient10 = c(4.4, 2.3, 1.4, 5.1)
row.names(genes) = c("ABC", "A2B", "DE5", "ZXY")
And this is how I need the genes df to look:
genes patient19 patient21 patient7 patient10
ABC 3.3 2.3 1.5 4.4
A2B 3.1 1.3 2.5 2.3
DE5 3.4 3.3 3.5 1.4
ZXY 3.6 4.3 4.5 5.1
Upvotes: 0
Views: 69
Reputation: 1127
You can do this using match
, the first argument is the labels you want to reorder, and the second argument is the desired order:
genes[, match(colnames(genes), rownames(pheno))]
The result of executing only match
is:
3 2 1 4
which is just the order in which you need the columns of the genes
dataframe, with respect to the order in the pheno
dataframe.
Upvotes: 1