Home > Back-end >  How to fix a CSV where the variable names are in each row in R
How to fix a CSV where the variable names are in each row in R

Time:01-14

I'm currently trying to work with this file: https://vote.nyc/sites/default/files/pdf/election_results/2021/20211102General Election/00000200000Citywide Mayor Citywide EDLevel.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?

CodePudding user response:

x <- read.csv("https://vote.nyc/sites/default/files/pdf/election_results/2021/20211102General Election/00000200000Citywide Mayor Citywide EDLevel.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")
  •  Tags:  
  • Related