UPDATED
I web scraped a table online that wasn't actually structured as a table. I managed to separate the characters into multiple rows, but for future reference, would like to know of a more efficient way to do this for larger data sets.
I also was able to get everything into one column, but the entire code is wildly inefficient. Any suggestions for improvement?
library(rvest)
library(tidyverse)
library(dplyr)
url = "https://www.ncsl.org/research/health/state-laws-and-legislation-related-to-biologic-medications-and-substitution-of-biosimilars.aspx"
webpage=read_html(url)
mandatory_2014 = webpage %>%
html_element(css = "#dnn_ctr84472_HtmlModule_lblContent > div > table:nth-child(15)") %>%
html_table()
mandatory_2014 = data.frame(mandatory_2014)
df = mandatory_2014 %>%
mutate(X1=strsplit(X1, "\n\n\t\t\t")) %>%
unnest(X1) %>%
mutate(X2=strsplit(X2, "\n\n\t\t\t")) %>%
unnest(X3)%>%
mutate(X3=strsplit(X3, "\n\n\t\t\t")) %>%
unnest(X3)
df = df[-c(2)]
df = stack(df)
df = df[-c(2)]
df = data.frame(df[!duplicated(df),])
df = rename(df, States = df..duplicated.df....)