Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replace current variable names and move them into rows

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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"))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading