Name
SPVM::R - Porting R language Features
Description
R class in SPVM is a port of the R language features.
Features
N-dimensional arrays and their operations, such as addition, subtraction, multiplication, division, modulo(or remainder), sum, max, min, mean, cumulative sum, cumulative product, trigonometric functions, exponential functions, log functions.
Support of sequence creation, repetition, slice, order, sort of a n-dimensional array.
Various types of n-dimensional arrays(byte, short, int, long, float, double, float complex, double complex, string, variable-length string, datetime).
Support of NA representation.
Data frame and its operations.
Usage
Math and Matrix Examples
use R::OP::Double as DOP;
use R::OP::Matrix::Double as DMOP;
# Scalar
my $sca1 = DOP->c((double)3);
# Vector
my $vec1 = DOP->c([(double)1, 2]);
my $vec2 = DOP->c([(double)3, 4]);
# Addition
my $add = DOP->add($vec1, $vec2);
# Subtruction
my $sub = DOP->sub($vec1, $vec2);
# Scalar multiplication
my $scamul = DOP->scamul($sca1, $vec1);
# Absolute
my $abs_vec1 = DOP->abs($vec1);
my $abs_vec2 = DOP->abs($vec2);
# Trigonometric
my $sin_vec1 = DOP->sin($vec1);
my $cos_vec1 = DOP->cos($vec1);
my $tan_vec1 = DOP->tan($vec1);
# Innner product
my $dot = DOP->dot($vec1, vec2);
# Matrix(column major)
my $mat1 = DMOP->matrix([1, 0, 0, 1], 2, 2);
my $mat_ret = DMOP->mul($mat1, $vec1);
See also examples of matrix.
Data Frame Examples
Create a data frame.
use R::DataFrame;
my $data_frame = R::DataFrame->new;
Add colunns:
use R::OP::String as STROP;
use R::OP::Int as IOP;
use R::OP::Double as DOP;
use R::OP::Time::Pirce as TPOP;
$data_frame->set_col("Name" => STROP->c(["Mike, "Ken", "Yumi"]));
$data_frame->set_col("Age" => IOP->c([12, 20, 15]));
$data_frame->set_col("Height" => DOP->c([(double)160.7, 173.2, 153.3]));
$data_frame->set_col("Birth" => TPOP->c(["2010-10-15", "2000-07-08", "2020-02-08"]));
Get columns and rows:
$data_frame->slice(["Name", "Age"], [IOP->c([0, 1, 2])]);
Get rows using conditions:
# Age > 12
my $conditions = IOP->gt($data_frame->col("Age"), IOP->rep(IOP->c(12), $data_frame->nrow);
$data_frame->slice(["Name", "Age"], [$conditions->to_indexes]);
# Age > 12 && Height < 150.3
my $conditions = IOP->and(
IOP->gt($data_frame->col("Age"), IOP->rep(IOP->c(12), $data_frame->nrow)),
IOP->lt($data_frame->col("Height"), IOP->rep(IOP->c(150.3), $data_frame->nrow)),
);
$data_frame->slice(["Name", "Age"], [$conditions->to_indexes]);
Sort rows:
# Age asc
$data_frame->sort(["Age"]);
# Age desc
$data_frame->sort(["Age desc"]);
# Age asc, Height asc
$data_frame->sort(["Age", "Height"]);
# Age asc, Height desc
$data_frame->sort(["Age", "Height desc"]);
See also examples of data frames.
Tutorial
Modules
N-Dimension Array
N-Dimension Array Operations
Matrix Operations
Data Frame
Utilities
Wiki
Repository
Author
Yuki Kimoto kimoto.yuki@gmail.com
Copyright & License
Copyright (c) 2024 Yuki Kimoto
MIT License