-1

I'm working in R studio and I have multiple small csv files (about 500) which I only need three columns from and within those columns, only two rows. Can I create a loop or series of loops to first extract the columns then the rows (or vice versa) that I need and write to one new csv file? Is there a way to maintain the file names so I know which data set came from where?

I read what I could find and tried to piece together a script, but nothing seems to work for what I need.

r2evans
  • 77,184
  • 4
  • 55
  • 96
EJD
  • 9
  • 1
    I suggest you read https://stackoverflow.com/a/24376207/3358227 – r2evans May 14 '21 at 20:01
  • It would be easier to help if you create a small reproducible example along with expected output. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah May 15 '21 at 13:21

1 Answers1

0

We can get the datasets read into a list, create the row index and column index to extract based on row column index

files <- list.files('/path/to/your/directory', full.names = TRUE,
        pattern = '\\.csv$')
# // change accordingly
rind <- c(3, 4)
cind <- c(22, 134)
out_lst <- lapply(files, function(x) read.csv(x)[rind, cind])
akrun
  • 674,427
  • 24
  • 381
  • 486