Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. A B C
  2. 1 2 3
  3. 4 5 6
  4.  
  5. B C A
  6. 2 3 1
  7. 5 6 4
  8.  
  9. mydf <- data.frame(A = 1:2, B = 3:4, C = 5:6)
  10. matches <- data.frame(X = 1:3, Y = c("C", "A", "B"), Z = 4:6)
  11. mydf
  12. # A B C
  13. # 1 1 3 5
  14. # 2 2 4 6
  15. matches
  16. # X Y Z
  17. # 1 1 C 4
  18. # 2 2 A 5
  19. # 3 3 B 6
  20.  
  21. out <- mydf[matches$Y]
  22. out
  23. # C A B
  24. # 1 5 1 3
  25. # 2 6 2 4
  26.  
  27. library(data.table)
  28. setDT(mydf)
  29. mydf
  30. # A B C
  31. # 1: 1 3 5
  32. # 2: 2 4 6
  33.  
  34. setcolorder(mydf, as.character(matches$Y))
  35. mydf
  36. # C A B
  37. # 1: 5 1 3
  38. # 2: 6 2 4
  39.  
  40. df <- read.table(h=T, text="A B C
  41. 1 2 3
  42. 4 5 6")
  43. vec <- c("B", "C", "A")
  44. df[vec]
  45.  
  46. B C A
  47. 1 2 3 1
  48. 2 5 6 4
  49.  
  50. df[df.clust$mutation_id]
  51.  
  52. mydf <- data.frame(A = 1:2, B = 3:4, C = 5:6)
  53. # A B C
  54. # 1 1 3 5
  55. # 2 2 4 6
  56. matches <- c("B", "C", "A") #desired order
  57.  
  58. mydf_reorder <- mydf[,match(matches, colnames(mydf))]
  59. colnames(mydf_reorder)
  60. #[1] "B" "C" "A"
  61.  
  62. match(matches, colnames(mydf))
  63. #[1] 2 3 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement