How to use loop or apply function to transform multiple data to matrix?

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!

Is there any reason you want to transform each individual file to data matrix? Or do you want to first put all files together and then call data.matrix on a result?

Hi,

Yes, the data matrix format is required for importing the data to phyloseq tool with tax_table function.

I don't think I can put all the file together at this point...

Ok, then next question is why do you need to put into an environment? Why can't you use the result of lapply with another lapply to do what you need?

Since I don't have your data and you didn't provide a reprex, it's difficult to help with concrete examples of code, so hopefully my questions are at least helpful for you to get to the point where it's easier for you to see what should be done.

Hi,
I did try to use lapply for my files,
X <- lapply (temp, as.matrix)
But the output X is still a list, and I also can not uselapply to apply tax_table on a list...

I think, it will be easier if you first step through the solution for one file and then generalize it for the entire thing. So, if you only needed to do this for one file, how would your code look like?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.