码迷,mamicode.com
首页 > 其他好文 > 详细

Perl与R语言交互

时间:2014-07-07 17:41:16      阅读:580      评论:0      收藏:0      [点我收藏+]

标签: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";
}











Perl与R语言交互,布布扣,bubuko.com

Perl与R语言交互

标签:for   算法   new   res   ar   re   

原文地址:http://www.cnblogs.com/datacatcher/p/3813616.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!