Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing a .csv with headers that show up as first row of data

Tags:

import

r

header

row

I'm importing a csv file into R. I read a post here that said in order to get R to treat the first row of data as headers I needed to include the call header=TRUE.

I'm using the import function for RStudio and there is a Code Preview section in the bottom right. The default is:

library(readr)
existing_data <- read_csv("C:/Users/rruch/OneDrive/existing_data.csv")
View(existing_data)

I've tried placing header=TRUE in the following places:

  • read_csv(header=TRUE, "C:/Users...)
  • existing_data.csv", header=TRUE
  • after 2/existing_data.csv")

Would anyone be able to point me in the right direction?

screenshot

like image 302
heeby89 Avatar asked Oct 28 '25 12:10

heeby89


1 Answers

You should use col_names instead of header. Try this:

library(readr)
existing_data <- read_csv("C:/Users/rruch/OneDrive/existing_data.csv", col_names = TRUE)

There are two different functions to read csv files (actually far more than two): read.csv from utils package and read_csv from readr package. The first one gets header argument and the second one col_names.

You could also try fread function from data.table package. It may be the fastest of all.

Good luck!

like image 63
Aleksandr Meshkov Avatar answered Oct 31 '25 03:10

Aleksandr Meshkov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!