简体   繁体   中英

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, ]

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 } .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM