Replace current variable names and move them into rows

28 views Asked by At

After extracting tables from a PDF using tabulizer, my table looks like:

A King Blue
D Queen Red
T Prince Black

I want to move the variable names down as observations and replace them with a vector of strings with the actual column names:

letter rank colour
A King Blue
D Queen Red
T Prince Black
1

There are 1 answers

0
Maël On BEST ANSWER
df <- rbind(names(df), df)
colnames(df) <- c("letter", "rank", "colour")

output

  letter   rank colour
1      A   King   Blue
2      D  Queen    Red
3      T Prince  Black

dplyr style:

df %>% 
  rbind(colnames(.), .) %>% 
  set_names(c("letter", "rank", "colour"))