Hi all,
I would like to transform 96 .txt files to matrix in R with data.matirx
I think I need a loop or using apply
function to process the files.
Here is part of input data in one file
Domain Phylum Class Order
OTU10001 Fungi Ascomycota Dothideomycetes Capnodiales
OTU10004 Fungi Ascomycota Dothideomycetes Pleosporales
And the code for single files:
BC76_OTU <- data.matrix(BC76.frequencytable)
I am trying to process all the files with data.matirx and write out each file to the environment with the following code:
Feature_to_matrix <- function(x) {
x <- as.matrix (files)
return(x)
}
files <- list.files(path="path to directory", pattern="*.txt", full.names=TRUE, recursive=FALSE)
lapply(files, function(Feature_to_matrix) {
t <- read.table(Feature_to_matrix, header=TRUE, row.names=1, sep="")
out <- t
})
But this code doesn't generate output files to the R environment.
I also try to write a loop for it
temp = list.files(pattern="*.txt"
for (i in 1:length(temp)) {
sample[i] <- read.csv(temp[i], header = TRUE,row.names=1,sep = "") write.matrix(sample[i]) }
but get an error as follow
Error in sample[i] <- read.csv(temp[i], header = TRUE, row.names = 1, : object of type 'closure' is not subsettable
Any suggestions for how to modify the code?
Thanks in advance!