Question: (Closed) Combining multiple files into a single text file in R
0
gravatar for bisansamara
10 months ago by
bisansamara10
bisansamara10 wrote:

I have several matlab files which I need to read in R, and combine them into a single text file for further analysis. Each matlab file has 3 columns, and a different number of rows. Each column represents a numeric parameter to describe a single cell, and each row in an observation at a different time point.

I attempted using the code below, but the problem is it combines all rows of a matlab file into columns in the combined file, ruining the order of data. An example to illustrate this is here explanation Of course entries of this example are just to explain the idea.

Current code:

setwd("C:/Users/Administrator/Documents") #define directory with all files to be combined
library(R.matlab) #load the library needed to read matlab files

all_files = list.files() #list all files in that directory

dataFiles = lapply (all_files, readMat) #read all files in the directory using readMat function

dataFiles_converted <- matrix(unlist(dataFiles), ncol = 3, byrow = FALSE) #list to matrix conversion

write.table(dataFiles_converted,"AllFilesCombined.txt", sep = "\t", row.names = FALSE, col.names = c("parameter1","parameter2","parameter3")) #write all the combined data to a tab delimited text file

Any suggestions of how to edit this code so I get a tidy combined text file?

image to explain: https://ibb.co/fwbnV8

code matlab datascience R • 843 views
ADD COMMENTlink modified 10 months ago • written 10 months ago by bisansamara10
1

We need to know the structure of you data, could you post the output of: head(dataFiles[[1]]) and str(dataFiles[[1]]). Then you can get valid answers.

ADD REPLYlink written 10 months ago by zx87547.1k

yes, strongly concur with this! I just happened to wrangle with a readMat output recently and they are tricky creatures. The more informatin you can give us about the result of readMat, the more likely we're able to help

EDIT: Can you also show the result of reading in just one file (instead of the full list of files)?

ADD REPLYlink modified 10 months ago • written 10 months ago by Friederike3.6k

How about "dataFiles_converted=do.call(rbind,dataFiles)"?

ADD REPLYlink written 10 months ago by Tom_L310

Still doesn't work. Columns in matlab files become rows in text file.

ADD REPLYlink modified 10 months ago • written 10 months ago by bisansamara10

That is weird, I frequently use "do.call" with input similar to the one you show in the picture. "rbind" should definitely works as you just want to append file contents. If you have a problems with columns/row transition, can you try a matrix transposition : "t()"?

ADD REPLYlink written 10 months ago by Tom_L310

Doesn't work, because multiple columns from matlab file could be joined together in the combined text file. It's not like each single column become a single row.

ADD REPLYlink written 10 months ago by bisansamara10

That's not a bioinformatics question, is it?

ADD REPLYlink written 10 months ago by Michael Dondrup46k

This is not bioinformatics, just text formatting. However, it's not different from recurrent questions about BED or SAM formatting either. What's the limit?

ADD REPLYlink written 10 months ago by Tom_L310
1

Our requirement is that the relation to bioinformatics needs to be clearly stated in the question, BED and SAM are at least bioinformatics specific file formats, that is the minimum requirement (which is quite easy going). I don't see this fulfilled here. From my recollection of what we have discussed recently, simple text-processing (with or without claiming that some column contains "genes") was not good enough, but even this claim is missing here.

ADD REPLYlink modified 10 months ago • written 10 months ago by Michael Dondrup46k

It's about the formatting needed before analysis.

ADD REPLYlink modified 10 months ago • written 10 months ago by bisansamara10

Honestly, I fail to see how this satisfies a relation to bioinformatics (and then it has to be assumed that this is due to how the question is written).

ADD REPLYlink modified 10 months ago • written 10 months ago by Michael Dondrup46k

Hello bisansamara!

We believe that this post does not fit the main topic of this site.

It is unclear what you are asking and if the context is within bioinformatics after all. Please explain the relation to bioinformatics to enable us to open the question again.

For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.

If you disagree please tell us why in a reply below, we'll be happy to talk about it.

Cheers!

ADD REPLYlink written 10 months ago by Michael Dondrup46k
Please log in to add an answer.
The thread is closed. No new answers may be added.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1494 users visited in the last hour