r - Reorder all the columns in a data table based on Other files rows -
i have 1 data.table(dt1)
has ids sorted criteria.
1. id10 2. id7757 3. id75340 4. id999 5. id5498
and 2nd data.table(dt2)
id's of first data.table
column names.
source id7757 id8948 id5498 id999 id10 id75340 source1 32 87 643 8676 34 10 source2 65 32 876 9457 8 777 source3 64 666 99 222 66 222
how can short columns of 2nd data.table
based on order of 1st data.table
? meaning id on 1st position should 1st column, 2nd position~2nd column etc...
the output file should follows:
source id10 id7757 id75340 id999 id5498 source1 34 32 10 8676 643 source2 8 65 777 9476 32 source3 66 64 222 222 666
how can done in r?
it's straight forward this. turns out dataset faulty.
so first sanitize datasets people!
the solution following:
all.sorted <- read.table("sorted.txt", header=t, sep=' ', na.strings="n/a") sorted.ids <- unlist(all.sorted$id) sorted.ids.sanitized <- make.names(sorted.ids) sorted.variants <- sorted.ids.sanitized # step check if , keep copy of original sorted.variants <- as.character(sorted.variants) new.order <- dt2[, sorted.variants]
Comments
Post a Comment