0

It is my first time posting on here so please bear with me.

I am supposed to load this dataset into R, and it is giving me lots of problems due to how it is formatted. As you can see in the screenshot of the CSV file in Excel, there are two headers. The first header row indicates the political party, and the second header row indicates the number of votes the party received, and the percentage of votes the party received.

I have managed to import this into R using the read.csv function, and skipping the first two lines. But then the header automatically becomes the second row, and I loose the values of the political party (which is not useful as I will need that later on)(see here)

I also tried reading it in with just the first header row, and then pasting the second header row. The issue here is that it only works for every other column (see here)

data_test <- read.csv("./electoral_data/municipal/2011/IBESTAT_I3101_430111_2023_02_06.csv",
                      sep = ";")
names(data_test) <- paste(names(data_test), data_test[1, ], sep = "_")

Does anyone know what I can do to fix this?

Thanks so much.

I have tried reading the files in different ways, but am unsure of how to actually merge these headers.

  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Please [do not post code or data in images](https://meta.stackoverflow.com/q/285551/2372064) – MrFlick Feb 06 '23 at 19:05
  • Some useful discussions here - https://stackoverflow.com/questions/17797840/reading-two-line-headers-in-r/17798282 – thelatemail Feb 06 '23 at 21:18

0 Answers0