How to set colnames and rownames when you read bunch of files from a directory into R? -
I have a lot of files in it. I can read all the files and select the second column of each file and I am trying to use Rbiind to make the matrix. But the problem is that after creating the matrix it has no alias and row name, in fact the row name should be the filename I read and rbind is the second column. Columns should be the first column of any of the file.
Here's my attempt:
nm < - list.files (path = "path / to / file") Matrix & lt; -do.call (rbind, lapply (nm, function (x) read.table (file = x) [, 2]))
Simulated data setup (such as all your files in the testdir
directory)
my.data & lt; - Indometh write.csv (my.data, file = "testdir / test1.csv", line.name = FALSE) my.data $ time & lt; - my.data $ time + 1 write.csv (my.data), File = "testdir / test2.csv", row.names = FALSE) my.data $ time & lt; - my.data $ time + 1 write.csv (my.data, file = "testdir / test3.csv",
Then some changes are required in your cycle
< Pre nm & lt; - list.files (path = "testdir") my .file & lt; - paste ("testdir", nm, sep = "/") MyDataFrame & lt; -do.call Cbind, lapply (my.file, function (x) {col2name & lt; - gsub ("\\ .. + $", "", basename (x)) my.col <- data.frame (read. Csv (file = x) [, 2]) no Mes (my.col) <- col2name my.col})) MyDataFrame
Here it read.csv < / Code>, customize it according to your needs :)
HH, Luca
Comments
Post a Comment