Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- A B C
- 1 2 3
- 4 5 6
- B C A
- 2 3 1
- 5 6 4
- mydf <- data.frame(A = 1:2, B = 3:4, C = 5:6)
- matches <- data.frame(X = 1:3, Y = c("C", "A", "B"), Z = 4:6)
- mydf
- # A B C
- # 1 1 3 5
- # 2 2 4 6
- matches
- # X Y Z
- # 1 1 C 4
- # 2 2 A 5
- # 3 3 B 6
- out <- mydf[matches$Y]
- out
- # C A B
- # 1 5 1 3
- # 2 6 2 4
- library(data.table)
- setDT(mydf)
- mydf
- # A B C
- # 1: 1 3 5
- # 2: 2 4 6
- setcolorder(mydf, as.character(matches$Y))
- mydf
- # C A B
- # 1: 5 1 3
- # 2: 6 2 4
- df <- read.table(h=T, text="A B C
- 1 2 3
- 4 5 6")
- vec <- c("B", "C", "A")
- df[vec]
- B C A
- 1 2 3 1
- 2 5 6 4
- df[df.clust$mutation_id]
- mydf <- data.frame(A = 1:2, B = 3:4, C = 5:6)
- # A B C
- # 1 1 3 5
- # 2 2 4 6
- matches <- c("B", "C", "A") #desired order
- mydf_reorder <- mydf[,match(matches, colnames(mydf))]
- colnames(mydf_reorder)
- #[1] "B" "C" "A"
- match(matches, colnames(mydf))
- #[1] 2 3 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement