function [] = Rsave(filename,data,groups); % Rsave - Convert a matrix of predictors (data) and vector of group labels % (groups) from Matlab variables to a text file that can be loaded into % R as a data.frame and subsequently analyzed with statistical classifiers. % % Use: Rsave(filename,data,groups); % % Mark Laubach (mark.laubach@yale.edu), September 2002 fid0 = fopen(filename,'w'); [T,N] = size(data); fprintf(fid0,['"' filename '" <- data.frame(\n']); fprintf(fid0,'\n\n'); for n = 1:N, if n < 10, fprintf(fid0,'\nF00'); fprintf(fid0, num2str(n) ); fprintf(fid0,' = c(' ); elseif n <100, fprintf(fid0,'\nF0' ); fprintf(fid0, num2str(n) ); fprintf(fid0,' = c(' ); else, fprintf(fid0,'\nF' ); fprintf(fid0, num2str(n) ); fprintf(fid0,' = c(' ); end fprintf(fid0,['%6.4f,'],data(1:end-1,n)); fprintf(fid0,['%6.4f),\n'],data(end,n)); end fprintf(fid0,'\nClass = factor(c('); fprintf(fid0,'%1.0f,',groups); uniqueGroups = unique(groups); fprintf(fid0,'),label = c('); for i = 1:size(uniqueGroups) fprintf(fid0,'%d,', uniqueGroups(i)); % fprintf(fid0,'"%s",', num2str(uniqueGroups(i))); end fprintf(fid0,')))'); fclose(fid0);