Search code examples
rcsvdata-cleaning

How to fix a CSV where the variable names are in each row in R


I'm currently trying to work with this file: https://vote.nyc/sites/default/files/pdf/election_results/2021/20211102General%20Election/00000200000Citywide%20Mayor%20Citywide%20EDLevel.csv

The way the file is written has the variable names with the corresponding values in the same row, as in

"AD","ED","County","EDAD Status","Event","Party/Independent Body","Office/Position Title","District Key","VoteFor","Unit Name","Tally","65","001","New York","IN-PLAY","General Election 2021 - 11/02/2021",,"Mayor","NYC",1,"Public Counter",160 "AD","ED","County","EDAD Status","Event","Party/Independent Body","Office/Position Title","District Key","VoteFor","Unit Name","Tally","65","001","New York","IN-PLAY","General Election 2021 - 11/02/2021",,"Mayor","NYC",1,"Manually Counted Emergency",0

and so on.

How do we convert this to normal dataframe format in R?


Solution

  • x <- read.csv("https://vote.nyc/sites/default/files/pdf/election_results/2021/20211102General%20Election/00000200000Citywide%20Mayor%20Citywide%20EDLevel.csv")%>%
      select(-c(1:11))
    
    colnames(x) <- c("AD","ED","County","EDAD Status","Event","Party/Independent Body","Office/Position Title","District Key","VoteFor","Unit Name","Tally")