dplyrtabulizer

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

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"))