Off topic:Combining multiple files into a single text file in R
0
0
Entering edit mode
5.9 years ago
bisansamara ▴ 20

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

R matlab datascience code • 4.1k views
ADD COMMENT
This thread is not open. No new answers may be added
Traffic: 2971 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6