Perl與R語言互動

來源:互聯網
上載者:User

標籤: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";
}











相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.