Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More Columns than Column Names?

Tags:

r

I am trying to read in a file that has 24 column headers, but 14 additional null values. See link: https://www.elections.il.gov/ElectionInformation/CandDataFile.aspx?id=51

Whenever I run the code below, I get the message

"more columns than column names."

I feel like the answer is probably simple. Any idea?

candidates <- read.csv(file = "candidates.txt", sep = ",", 
                        header = TRUE, stringsAsFactors = FALSE)
like image 222
Aaron Feldman Avatar asked Dec 19 '22 15:12

Aaron Feldman


1 Answers

One solution is to read the headers only with one command, the data without header in a second, and then delete excess columns and set the names.

NAMES <- read.table("candidates.txt", nrow = 1, stringsAsFactors = FALSE, sep = ",")
DATA <- read.table("candidates.txt", skip = 1, stringsAsFactors = FALSE, sep = ",")
DATA <- DATA[, 1:24]
names(DATA) <- NAMES 
like image 67
Richard Telford Avatar answered Dec 21 '22 04:12

Richard Telford