#!usr/bin/perl
use Statistics::R;
#---new函数
my $r=Statistics::R->new();
#---send,read函数
$r->send(qq‘a=c(1,2,3,4); b=mean(a);\n c=sqrt(b) ;print(c)‘);
my $result=$r->read;
print $result,"\n";
#---转换称数值格式
$result=~s/\[\d\]\s+(\d+)/$1/g;
print $result+2;
print "\n";
#set,get,run
my $value=12;
$r->set(‘x‘,$value);
my $key=$r->get(‘x‘);
print $key,"\n";
$r->run(q‘y=x+4‘);
my $data=$r->get(‘y‘);
print $data;
my @str1=(5,6,7,10);
my @str2=(1,7,3,5);
$r->set(‘x1‘,\@str1);
$r->set(‘x2‘,\@str2);
$r->run(q`cov <- cov(x1,x2)`);
$r->run(q`x22 <- x1^2`);
my $cov=$r->get(‘cov‘);
my $x22=$r->get(‘x22‘);
print $cov,"\n";
print "@$x22\n";
#做一个简单的矩阵操作
my @str11=(5,6,7,10);
my @str22=(1,7,3,5);
$r->set(‘x‘,\@str11);
$r->set(‘y‘,\@str22);
$r->run(q‘z=c(x,y)‘);
my $uu=$r->get(‘z‘);
print scalar @$uu,"\n";
foreach my $ss(@$uu){
print "$ss\n";
}
#做一个简单的kmeans算法
$r->run(q‘library(class)‘);
$r->run(q‘x=1:10;y=kmeans(x,4);s=y$cluster‘);
my $result1=$r->get(‘s‘);
print "@$result1\n";
foreach my $key(@$result1){
print $key,"\n";
}
原文地址:http://www.cnblogs.com/datacatcher/p/3813616.html