you can specify column names and how many lines to skip directly in read.csv
.
For example below:
read.csv(file = "yourfile.csv",
skip = 3, # How many lines you want to skip when you read it
header = FALSE, # Skip the header too
col.names = c("Student", "Score"), # Supply your own column names
stringsAsFactors = FALSE
)
For a full reproducible example:
# Generate data in text format
raw_file <-
'
Category: All categories
Name, Marks
Mohit, 100
Raman, 71
Kaveri, 45
William, 42
Ram Pravesh, 37
'
# make a temp file to place data
temp_file <- tempfile(fileext = ".csv")
# write the temp file
writeLines(raw_file,con = temp_file)
read.csv(file = temp_file,
skip = 4, # How many lines you want to skip when you read it
header = FALSE, # Skip the header too
col.names = c("Student", "Score"), # Supply your own column names
stringsAsFactors = FALSE
)
This will yield the following:
Student Score
1 Mohit 100
2 Raman 71
3 Kaveri 45
4 William 42
5 Ram Pravesh 37
You also mentioned reading in multiple files:
# Get all the files that end in a csv in a given folder that you specify
files_to_read <- list.files(path = "some_path", pattern = ".csv", full.names = T)
# I like `purrr` here because it makes a few things easier
# Read in and row bind all csv to return a single data frame
library(purrr)
out <- map_dfr(files_to_read, ~read.csv(file = .x,
skip = 4, # How many lines you want to skip when you read it
header = FALSE, # Skip the header too
col.names = c("Student", "Score"), # Supply your own column names
stringsAsFactors = FALSE
))