Deepesh
Deepesh

Reputation: 840

How to delete the first row from a .csv file?

I have 500 csv files in which first row of each file has the name of that file. I wish to exclude the file name from my data.

I have tried this but it is not working:

temp = list.files(pattern="*.csv")
myfiles = lapply(temp, read.delim)
myfiles = myfiles[-1, ]

Upvotes: 0

Views: 2699

Answers (1)

Jacob H
Jacob H

Reputation: 4513

This is clearly a R question. However, I thought I would suggest a Unix approach. Unix will be much faster than R for this task and IMO it is the more natural tool. If you have Windows you'll have to download cygwin. This may be a headache, however, with only minimal knowledge, Unix is a very powerful tool. There are essentially two approaches to your problem:

First Approach

You can modify each file so that the first row is removed. This means that your original .csv will no longer exist.

sed -i 1d *.csv

Second Approach

The first approach is problematic. You might want to keep the original files. If this is the case you need to remove the -i flag from the above code. We will also need to use a for loop so we can name each of the new files.

for f in *.csv; do sed 1d $f > new_$f; done

A for loop in Unix is kinda like an R for loop, except do and done replace { and }.

Upvotes: 1

Related Questions