jessica
jessica

Reputation: 1355

Designating Other than First Row as Headers in R

I have data in a .csv that I am trying to pull into R. Using the following command:

data=read.table("C:\\Users\\Riemman\\Desktop\\IWM_Minute_Data.csv",header=TRUE,sep=",",skip=2)

the data in the CSV file is structured so that the first row in the row in the data file is empty, the second row are the headers and the data starts at the third row. How do I tell R to designate the second row and not the first as the header row??

Upvotes: 1

Views: 5661

Answers (3)

MrFlick
MrFlick

Reputation: 206187

Just use the skip= parameter to skip lines at the beginning of the file. That will allow you to keep header=T and have the right col names

data <- read.csv("IWM_Minute_Data.csv",skip=1)

Upvotes: 6

Robert Krzyzanowski
Robert Krzyzanowski

Reputation: 9344

Why not remove the first row?

filename <- "C:\\Users\\Riemman\\Desktop\\IWM_Minute_Data.csv"
tmp <- readLines(filename)[-1]
writeLines(tmp, filename)
data <- read.table(tmp, header = TRUE, sep = "")

Upvotes: 2

JeremyS
JeremyS

Reputation: 3525

You can read it in without headers then add them later

data <- read.csv("C:\\Users\\Riemman\\Desktop\\IWM_Minute_Data.csv",header=F)
colnames(data) <- data[2,]
data <- data[c(-1,-2),]

Upvotes: 1

Related Questions