0

I'm curious if it is possible to create a vector of column names, that can be used as a reference in the arrangement of a data.frame's columns?

c <- c("a", "b", "c")
d <- c(1, 2, 3)
e <- c(4, 5, 6)
f <- c(12, 33, 22)

rf <- data.frame(c, d, e, f)

print(rf)

  c d e  f
1 a 1 4 12
2 b 2 5 33
3 c 3 6 22

colnamesvec <- c("c", "e", "f", "d") #to be compared against

But I want my dataframe rf to compare against colnamesvec, and produce this result instead.

  c e  f d
1 a 4 12 1
2 b 5 33 2 
3 c 6 22 3

Thank you so much!

Javier
  • 730
  • 4
  • 17

0 Answers0