Question

I want to read 300 *.csv format arrays discarding first row and first column. I know if I use "header=TRUE" in "read.csv" it would not consider the first row. For example:

  mydta.without.first.row <- read.csv("C:\\Users\\Desktop\\myarray.csv",header=FALSE)

Is there any argument that I can use and discard the first column as well?

Was it helpful?

Solution

You can load your 300 data sets in a list :

I would first put all my .csv files in a directory, and then :

gdir <- "dir-containing-your-300-files"
fn <- list.files(gdir, pattern="*.csv", full.names=F)
ldf <- lapply(fn, function(x) read.csv(x,header=F)[-1,-1])

You'l end up with a list of 300 data.frames without the first row and column. Each list element will have a name (your file names) attach to it.

If you want to write the new data.frames in files (with new.filename.csv as file names).

mapply(write.table,x=ldf,file=paste("new",fn,sep="."),MoreArgs=list(row.names=F,col.names=F,sep=","))

This will write new files, so you won't erase any of your original ones. You might have to tweak the command to suit your need.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top