フィルターのクリア

How can I export a large list from R to Matlab

45 ビュー (過去 30 日間)
Behrooz Daneshian
Behrooz Daneshian 2023 年 2 月 2 日
コメント済み: Heath 2023 年 4 月 28 日
Hi all,
I have created a large list in R and now, I intend to use this list in matlab. Can anyone help me in this regard?
  3 件のコメント
Rik
Rik 2023 年 2 月 3 日
Can you provide a small example of the input and the intended output? That would also explain what a tibble would be.
Behrooz Daneshian
Behrooz Daneshian 2023 年 2 月 3 日
Would you please see the attached jpgs? the daily_t is showing the created list that each element of this list is a data frame(or tibble). I think tibble in R is the same data structure as table. tibble jpg represents each element of list " daily_t.

サインインしてコメントする。

採用された回答

Shushant
Shushant 2023 年 2 月 13 日
I understand you are trying to convert a list of Data Frame from R (as shown in the image "daily_t.JPG") to a table in MATLAB (as shown in the image tibble.JPG). I am assuming the fact that the list of data frames in R all have 3 common columns which are 'id', 'date' and 'TAVG' with different rows, and you want to get this data as a single table in MATLAB with 3 columns and all the rows. To achieve this, I have also created a small list of data frame in r as shown below.
Code for R:
#Create data frame with 2 rows and 3 columns
df1 = data.frame(
id = c('CA0012303','CA0012305'),
date = c("2007-06-22", "2007-06-26"),
TAVG = c(-0.85, 3.65)
)
# Create another data frame with 3 rows and the same 3 columns
df2 = data.frame(
id = c('CA003303', 'CA001533', 'CA556632'),
date = c("2007-07-22", "2007-07-24", "2006-07-24"),
TAVG = c(7.7, 7.86, 3.5)
)
# Create list of data frame using list()
listOfDataframe = list(df1, df2)
print(listOfDataframe)
Output:
[[1]]
id date TAVG
1 CA0012303 2007-06-22 -0.85
2 CA0012305 2007-06-26 3.65
[[2]]
id date TAVG
1 CA003303 2007-07-22 7.70
2 CA001533 2007-07-24 7.86
3 CA556632 2006-07-24 3.50
Now, I used the "bind_rows" function from the "dplyr" library to bind the list of data frames and convert them into one dataframe.
Code for R:
# Installing and importing the dplyr package
install.packages("dplyr")
library("dplyr")
# Combining the data frames
finalDset = bind_rows( listOfDataframe)
print(finalDset)
Output:
id date TAVG
1 CA0012303 2007-06-22 -0.85
2 CA0012305 2007-06-26 3.65
3 CA003303 2007-07-22 7.70
4 CA001533 2007-07-24 7.86
5 CA556632 2006-07-24 3.50
The final step in R is to export the "finalDset" as a MAT file to accomplish this we use the function "writeMat" from the library "R.matlab".
Code for R:
# installing and importing the R.matlab package
install.packages(c('R.matlab'), repos='http://cran.us.r-project.org')
library(R.matlab)
writeMat(con="myMat.mat", myTable = finalDset)
This will create a new MAT file called "myMat.mat" in your current working directory. Copy that file into your MATLAB working directory and load the file to your workspace. Once, loaded we will see a new variable called "myTable" created in our Workspace with the datatype as struct. We will then convert "myTable" to a table using the function "struct2table". Finally, we will convert the "id" and "date" from cell to string to get the desired result as shown in image "tibble.JPG"
Code for MATLAB with outputs:
>> load("myMat.mat")
>> myTable
myTable =
struct with fields:
id: {5×1 cell}
date: {5×1 cell}
TAVG: [5×1 double]
>> myTable = struct2table(myTable)
myTable =
5×3 table
id date TAVG
_____________ ______________ _____
{'CA0012303'} {'2007-06-22'} -0.85
{'CA0012305'} {'2007-06-26'} 3.65
{'CA003303' } {'2007-07-22'} 7.7
{'CA001533' } {'2007-07-24'} 7.86
{'CA556632' } {'2006-07-24'} 3.5
>> myTable.id = char(myTable.id)
myTable =
5×3 table
id date TAVG
_________ ______________ _____
CA0012303 {'2007-06-22'} -0.85
CA0012305 {'2007-06-26'} 3.65
CA003303 {'2007-07-22'} 7.7
CA001533 {'2007-07-24'} 7.86
CA556632 {'2006-07-24'} 3.5
>> myTable.date = char(myTable.date)
myTable =
5×3 table
id date TAVG
_________ __________ _____
CA0012303 2007-06-22 -0.85
CA0012305 2007-06-26 3.65
CA003303 2007-07-22 7.7
CA001533 2007-07-24 7.86
CA556632 2006-07-24 3.5
  1 件のコメント
Heath
Heath 2023 年 4 月 28 日
That's a bold assumption

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by