標籤:for 演算法 new res ar re
#!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";
}