0

is there a way to extract column names from a df and convert into a vector? In fact, what I am trying to do is rbing two df's, the second has no names so returning a name matching error? Maybe there is an easier way to copy the df1 column names to df2 so rbind will work?

  • Try `names(df2) <- names(df1)` – Sotos Aug 12 '20 at 13:44
  • In general, `names(df)` or `colnames(df)` both work to get the column names as a vector. And as the earlier comment shows, you can use them to modify the names as well, e.g., `names(df) <- vector_of_new_names` or `names(df)[3] <- "new name for third column"`. – Gregor Thomas Aug 12 '20 at 14:23

1 Answers1

0

In one line:

df3 <- rbind(df1, setNames(df2, names(df1)))
Mikael Jagan
  • 9,012
  • 2
  • 17
  • 48