Can MATLAB read 'R' data files (*.Rda) directly?

답변 (2개)

Peter
Peter 2023년 6월 9일

1 개 추천

If you have Python installed on your system, you can use use the Matlab python call functionality to read in such R files.
Firstly, ensure that you have installed the Python package pyreadr onto your system; this package reads/writes R RData and Rds files into/from pandas data frames, and does not require R to have been installed, not any other external dependencies installed.
Then you can load in matlab via the command:
dict = py.pyreadr.read_r(<path to rda file>);
As it stands the variable "dict" will be a Python OrderedDict, so to use data it contains you need to create a matlab array as follows:
result=double(dict{<key>}.values);
where <key> is the string defining which key of the pandas frame dict you wish to extract.
If in doubt, check which keys the imported panda frame has by running the command:
dict.keys()
Once you have found the key of interest, you may further check the complete properties of the data stored under this key by running the command:
dict{<key>}
Pierre Benoit
Pierre Benoit 2014년 9월 17일
편집: Pierre Benoit 2014년 9월 17일

0 개 추천

The only other topic I found about this was this.

댓글 수: 2

Richard
Richard 2014년 9월 17일
Thanks, Pierre. I ended up reading them in to 'R', then saving as a CSV which imports easily into MATLAB. Since I am new to 'R', it took me some time to generate the appropriate code. Here is an example:
> load(“kidneydata.Rda”) % from Brad Efron's web site
> dim(kidneydata) % returns 157 x 2
> A <- t(kidneydata) % transpose array
> dim(A) % returns 2 x 157
> write(A,"kidneydata.csv",ncolumns = 2,sep = ",") % save as CSV file
> write(A,"kidneydata.csv",ncolumns = 2,sep = "\t") % to save as TSV file
Andrew
Andrew 2020년 9월 22일
Now in 2020 with R studio 1.1.456, the 'write' function croaked with a mysterious "Error in cat...argument 1 (type 'list') cannot be handled by cat" error, but:
write.csv(variable,"filename");
worked just fine.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

태그

질문:

2014년 9월 16일

답변:

2023년 6월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by