Reputation: 81
The dat
columnnames corresponds with the values of one of the columns of dat.info
, namely dat.info$eset.sample
.
I reordered the dat
columnames and now want to reorder the rows of dat.info
in the same order as the column names in dat
.
Before:
dat
| | 3 | 2 |1 |
| -------- | -------------- |-------------|-----------|
| x | Order |This |In |
| y | Way |Correct |The |
dat.info
| |eset.sample |
| -------| -------------- |
| now | 3 |
| order | 2 |
| same | 1 |
After:
dat
| | 1 | 2 |3 |
| -------- | -------------- |-------------|-----------|
| x | In |This |Order |
| y | The |Correct |Way |
dat.info
| |eset.sample |
| -------| -------------- |
| same | 1 |
| order | 2 |
| now | 3 |
> dput(dat[1,1:40])
structure(list(GSM97800 = 4701.5, GSM97803 = 4735, GSM97804 = 2863.9,
GSM97805 = 5350.2, GSM97807 = 4789.4, GSM97809 = 5837.8,
GSM97811 = 4446.7, GSM97812 = 4264.1, GSM97816 = 11011.5,
GSM97817 = 3832.6, GSM97820 = 5227.2, GSM97825 = 2935.6,
GSM97827 = 3561.5, GSM97828 = 10728.8, GSM97833 = 4156.6,
GSM97834 = 4278.4, GSM97840 = 5916.2, GSM97846 = 3136.6,
GSM97848 = 4415.7, GSM97849 = 3313.4, GSM97850 = 6011.4,
GSM97853 = 4107.2, GSM97855 = 7053.4, GSM97878 = 8196.2,
GSM97913 = 6006.6, GSM97932 = 2619.5, GSM97939 = 11699.2,
GSM97951 = 7718.5, GSM97957 = 12700.4, GSM97972 = 8816.4,
GSM97793 = 10178.1, GSM97795 = 7826.6, GSM97802 = 6857.8,
GSM97810 = 8255, GSM97815 = 8016.8, GSM97837 = 10350.9, GSM97843 = 10626.1,
GSM97890 = 8584.5, GSM97899 = 10615.1, GSM97910 = 12047.3), row.names = "1007_s_at", class = "data.frame")
> dput(dat.info[1:40,1])
c("GSM97800", "GSM97803", "GSM97804", "GSM97805", "GSM97807",
"GSM97809", "GSM97811", "GSM97812", "GSM97816", "GSM97817", "GSM97820",
"GSM97825", "GSM97827", "GSM97828", "GSM97833", "GSM97834", "GSM97840",
"GSM97846", "GSM97848", "GSM97849", "GSM97850", "GSM97853", "GSM97855",
"GSM97878", "GSM97913", "GSM97932", "GSM97939", "GSM97951", "GSM97957",
"GSM97972", "GSM97793", "GSM97795", "GSM97802", "GSM97810", "GSM97815",
"GSM97837", "GSM97843", "GSM97890", "GSM97899", "GSM97910")
dat.new <- cbind(dat[1:23], dat[24:30], dat[131:168], dat[31:49], dat[169:180], dat[50:130])
> dput(dat.new[1,1:40])
structure(list(GSM97800 = 4701.5, GSM97803 = 4735, GSM97804 = 2863.9,
GSM97805 = 5350.2, GSM97807 = 4789.4, GSM97809 = 5837.8,
GSM97811 = 4446.7, GSM97812 = 4264.1, GSM97816 = 11011.5,
GSM97817 = 3832.6, GSM97820 = 5227.2, GSM97825 = 2935.6,
GSM97827 = 3561.5, GSM97828 = 10728.8, GSM97833 = 4156.6,
GSM97834 = 4278.4, GSM97840 = 5916.2, GSM97846 = 3136.6,
GSM97848 = 4415.7, GSM97849 = 3313.4, GSM97850 = 6011.4,
GSM97853 = 4107.2, GSM97855 = 7053.4, GSM97878 = 8196.2,
GSM97913 = 6006.6, GSM97932 = 2619.5, GSM97939 = 11699.2,
GSM97951 = 7718.5, GSM97957 = 12700.4, GSM97972 = 8816.4,
GSM97799 = 9267.2, GSM97823 = 6581.8, GSM97824 = 11527.5,
GSM97830 = 12217, GSM97835 = 10019.1, GSM97838 = 4566.5,
GSM97841 = 6601, GSM97842 = 10721.4, GSM97854 = 4918.9, GSM97857 = 10116.9), row.names = "1007_s_at", class = "data.frame")
Upvotes: 0
Views: 959
Reputation: 2141
We can use order
on the colnames
and apply this to the dat.info
matrix:
dat <- matrix(c("Order","Way","This","Correct","In","The"),ncol=3)
dimnames(dat) <- list(c("x","y"),c("3","2","1"))
dat.info <- matrix(c(3,2,1),ncol=1)
dimnames(dat.info) <- list(c("now","order","same"),"eset.sample")
dat.new <- dat[,sort(colnames(dat))]
dat.info.new <- dat.info[order(colnames(dat)),,drop=F]
dat.new
1 2 3
x "In" "This" "Order"
y "The" "Correct" "Way"
dat.info.new
eset.sample
same 1
order 2
now 3
Without reproducible example an idea how it could work when your order in dat.new
is not based on some form of sort
:
ord <- match(colnames(dat),colnames(dat.new))
dat.info.new <- dat.info[ord,,drop=F]
Upvotes: 0
Reputation: 78917
In case the dat
dataframe is at it is:
You could try this:
dat %>%
mutate(id = row_number()) %>%
pivot_longer(
cols = -id,
names_to = "name",
values_to = "value"
) %>%
group_by(id) %>%
arrange(name, .by_group = TRUE) %>%
pivot_wider(
names_from = name
)
output:
`1` `2` `3`
<chr> <chr> <chr>
1 In This Order
2 The Correct Way
data:
structure(list(`1` = c("In", "The"), `2` = c("This", "Correct"
), `3` = c("Order", "Way")), row.names = c(NA, -2L), class = c("tbl_df",
"tbl", "data.frame"))
Upvotes: 2