问题描述
我有一个表数据类型的matlab .mat
文件,我想在R中导入.为此我正在使用'readMat',R正在将其读取为列表. >
之后,有没有办法将列表转换为R中的数据框或表格式?当我使用as.dataframe
时,出现以下错误:
Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, :
arguments imply differing number of rows: 5, 6, 1
我想到的一种可能的解决方法是从matlab中将表导出为.csv并将其读取到R中.但是,此解决方案对我不起作用,因为表中的某些列是1x700数组,而CSV中的这些列扩展到700列.
所以我的问题是,有直接或间接的方法可以在R中导入MATLAB表吗?
我相信最好的解决方案是按照您的建议从MATLAB中将表导出为CSV文件.
关于某些列的大小为1 * 700阵列,我建议在整洁的方式 -其中每一列代表一个变量,每一行代表一个观察值.1X700阵列的700列中的每一列是不同的变量,还是同一变量的不同值,也许是在不同的时间戳记上?我的猜测是,您可以使用外键以将每个新行连接到表中的现有行.
整齐的数据"的概念在MATLAB中使用较少,在MATLAB中通常使用行向量表示数据,但在R中被更广泛地接受(请参见" tidyverse "软件包以了解更多信息.
在MATLAB 中,将每个1X700数组拆分为700X2向量-一列将作为外键-数据表中该行的标识,第二列应为的值您的变量.如果原始数据中有n个观测值,则现在应该有一个大小数组(nX700,2).也将此数组另存为表.假设您现在在MATLAB中有两个表(您的原始表和您重塑的数据),请使用可写表:
writetable(table_1, 'table_1.csv')
writetable(table_2, 'table_2.csv')
然后,在R中,使用 read_csv :
读取表table_1 <- read_csv('table_1.csv')
table_2 <- read_csv('table_2.csv')
希望这会有所帮助.
I have a matlab .mat
file with table data type which I want to import in R. I am using 'readMat' for this and R is reading it as a List.
After that is there a way to convert the list into either a dataframe or table format in R? When I use as.dataframe
I get the following error :
Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, :
arguments imply differing number of rows: 5, 6, 1
A possible workaround I thought of is to export the table as a .csv from matlab and read it into R. But this solution does not work for me as some columns in the table are 1x700 arrays and in the CSV each these columns get expanded to 700 columns.
So my question is that is there a direct or indirect way to import a MATLAB table in R?
I believe that the best solution is to export the table from MATLAB as a CSV file, as you suggested.
Regarding the size of the certain columns being 1*700 arrays, I would recommend reshaping your data in a tidy way - where each column represents a variable, and each row represents an observation.Is each of your 700 columns of the 1X700 array a different variable, or is it a different value of the same variable, perhaps at a different timestamp? My guess is that you can split these arrays to a 700X1 array, and add an identification to the relevant row in the table data, using foreign keys to connect each new row to an existing row in the table.
This concept of "tidy data" is used less in MATLAB, where it is common to use row vectors to represent data, but is much more accepted in R (see the "tidyverse" packages for more about this).
In MATLAB, split each of the 1X700 arrays to 700X2 vectors - one column will be the foreign key - the identification of the row at the data table, and the second columns should be the value of your variable. If you have n observations in your original data, you should have now an array of size (nX700, 2). Save this array as a table as well. Assuming you now have in MATLAB two tables (your original table and your reshaped data), export them as CSV using writetable:
writetable(table_1, 'table_1.csv')
writetable(table_2, 'table_2.csv')
Then, in R, read the tables using read_csv:
table_1 <- read_csv('table_1.csv')
table_2 <- read_csv('table_2.csv')
Hopes this helps.
这篇关于如何在R中导入Matlab表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!