Setting names of df with character vector that is a different length

Viewed 558

I'm having trouble setting names to a df that I have scraped from a website. The html_table has a bunch of columns that don't have proper column names. When I go to use purrr::set_names I get an error message since the columns scraped, are a different length than the columns on the html_table. Is there a way to resolve this that anyone can think of? The missing set of columns appear to be duplicate data. They are identical to columns 5:10 in data. Example below:

library(tidyverse)
# url
url <- "https://legacy.baseballprospectus.com/pitchfx/leaderboards/index.php?hand=&reportType=avg&prp=P&month=&year=2019&pitch=FS&ds=&lim=0"

# get page from URL
page <-
  url %>%
  xml2::read_html()

# get df from page
data <-
  page %>%
  rvest::html_table(fill = TRUE) %>%
  .[[2]] %>%
  data.frame(stringsAsFactors = FALSE) %>%
  tbl_df()

# get column names
names <-
  page %>%
  rvest::html_node("thead") %>%
  rvest::html_nodes("th") %>%
  rvest::html_text() %>%
  stringr::str_trim()

# Add Column names since Scrape does not include
df <-
  data %>%
  purrr::set_names(names)
#> `nm` must be `NULL` or a character vector the same length as `x`
Created on 2019-10-18 by the reprex package (v0.3.0)
1 Answers

The issue is the length of names is not matching with the number of columns of the 'data'. According to ?set_names

set_names(x, nm = x, ...)

x- Vector to name.

... - Vector of names, the same length as x.

As it requires the same length and here it is not the case, it is throwing an error.

One option is to use rename_at. If we know the column position index or column names, pass that in the vars of rename_at and change the column names

library(dplyr)
data %>%
     rename_at(seq_len(length(names)), ~ names)
Related