CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-rpy2

Python interface to the R language (embedded R)

Pending
Overview
Eval results
Files

vectors-datatypes.mddocs/

Vector and Data Types

Comprehensive Python wrappers for R's vector types and data structures, providing seamless integration between Python and R data representations with automatic type conversion and Python-like operations.

Capabilities

Numeric Vectors

Python wrappers for R's numeric vector types with full mathematical operations support.

class IntVector(Vector):
    """
    R integer vector with Python list-like interface.
    
    Supports R integer operations, indexing, and conversion
    to/from Python integers and lists.
    """
    def __init__(self, iterable): ...

class FloatVector(Vector):
    """
    R numeric (double) vector with Python list-like interface.
    
    Supports R numeric operations, mathematical functions,
    and conversion to/from Python floats and lists.
    """
    def __init__(self, iterable): ...

class ComplexVector(Vector):
    """
    R complex vector with Python list-like interface.
    
    Supports complex number operations and conversion
    to/from Python complex numbers.
    """
    def __init__(self, iterable): ...

Logical and Character Vectors

Vectors for boolean and string data with R-specific behavior.

class BoolVector(Vector):
    """
    R logical vector with Python list-like interface.
    
    Handles R's three-valued logic (TRUE/FALSE/NA) and
    conversion to/from Python booleans.
    """
    def __init__(self, iterable): ...

class StrVector(Vector):
    """
    R character vector with Python list-like interface.
    
    Supports R string operations and conversion to/from
    Python strings and lists of strings.
    """
    def __init__(self, iterable): ...

class ByteVector(Vector):
    """
    R raw (byte) vector with Python bytes-like interface.
    
    Handles raw binary data in R, similar to Python bytes
    objects with R vector operations.
    """
    def __init__(self, iterable): ...

class FactorVector(Vector):
    """
    R factor vector for categorical data.
    
    Represents categorical variables with defined levels,
    similar to pandas Categorical or R factors.
    """
    def __init__(self, iterable, levels=None, ordered=False): ...
    
    @property
    def levels(self) -> StrVector:
        """Get factor levels."""
    
    @property 
    def ordered(self) -> bool:
        """Check if factor is ordered."""

Base Vector Class

Common functionality shared by all vector types.

class Vector(RObject):
    """
    Base class for R vectors with Python sequence interface.
    
    Provides common vector operations including indexing,
    slicing, length, and mathematical operations.
    """
    def __len__(self) -> int: ...
    def __getitem__(self, key): ...
    def __setitem__(self, key, value): ...
    def __iter__(self): ...
    
    @property
    def names(self) -> StrVector:
        """Get or set vector element names."""
    
    @names.setter
    def names(self, value): ...

Container Types

R lists and specialized container structures.

class ListVector(Vector):
    """
    R list with Python dict-like interface.
    
    Named lists behave like Python dictionaries while
    maintaining R list semantics and operations.
    """
    def keys(self): ...
    def values(self): ...
    def items(self): ...

class PairlistVector(Vector):
    """
    R pairlist wrapper for argument lists and environments.
    
    Used internally by R for function arguments and
    environment contents.
    """

Data Frames

R data.frame with pandas-like interface for tabular data.

class DataFrame(ListVector):
    """
    R data.frame with pandas-like interface.
    
    Provides column access, row/column operations, and
    integration with pandas DataFrames when available.
    """
    def __init__(self, data): ...
    
    @property
    def rownames(self) -> StrVector:
        """Get or set row names."""
    
    @rownames.setter
    def rownames(self, value): ...
    
    @property
    def colnames(self) -> StrVector:
        """Get or set column names."""
    
    @colnames.setter  
    def colnames(self, value): ...
    
    @property
    def ncol(self) -> int:
        """Number of columns."""
    
    @property
    def nrow(self) -> int:
        """Number of rows."""

Multi-dimensional Arrays

Matrices and higher-dimensional arrays with R semantics.

class Matrix(Vector):
    """
    Two-dimensional R matrix with mathematical operations.
    
    Supports matrix algebra, indexing by row/column,
    and conversion to/from NumPy arrays when available.
    """
    def __init__(self, array, nrow=None, ncol=None): ...
    
    @property
    def nrow(self) -> int:
        """Number of rows."""
    
    @property  
    def ncol(self) -> int:
        """Number of columns."""
    
    @property
    def rownames(self) -> StrVector:
        """Row names."""
    
    @property
    def colnames(self) -> StrVector:
        """Column names."""

class Array(Vector):
    """
    Multi-dimensional R array with arbitrary dimensions.
    
    Supports n-dimensional indexing and operations
    similar to NumPy arrays but with R semantics.
    """
    def __init__(self, array, dim=None): ...
    
    @property
    def dim(self) -> IntVector:
        """Array dimensions."""

Specialized Matrix Types

Type-specific matrix classes for each vector type.

class IntMatrix(Matrix, IntVector):
    """Integer matrix with mathematical operations."""

class FloatMatrix(Matrix, FloatVector):
    """Floating-point matrix with mathematical operations."""

class BoolMatrix(Matrix, BoolVector):
    """Boolean matrix with logical operations."""

class ComplexMatrix(Matrix, ComplexVector):
    """Complex number matrix with mathematical operations."""

class StrMatrix(Matrix, StrVector):
    """String matrix for text data organization."""

class ByteMatrix(Matrix, ByteVector):
    """Byte matrix for raw binary data organization."""

Specialized Array Types

Type-specific array classes for each vector type.

class IntArray(Array, IntVector):
    """Multi-dimensional integer array."""

class FloatArray(Array, FloatVector):
    """Multi-dimensional floating-point array."""

class BoolArray(Array, BoolVector):
    """Multi-dimensional boolean array."""

class ComplexArray(Array, ComplexVector):
    """Multi-dimensional complex number array."""

class StrArray(Array, StrVector):
    """Multi-dimensional string array."""

class ByteArray(Array, ByteVector):
    """Multi-dimensional byte array for raw data."""

Date and Time Vectors

R date and time representations with timezone support.

class DateVector(Vector):
    """
    R Date vector for calendar dates.
    
    Represents dates without time information,
    compatible with Python datetime.date objects.
    """
    def __init__(self, iterable): ...

class POSIXct(Vector):
    """
    R POSIXct vector for date-time as continuous time.
    
    Calendar time representation with timezone support,
    compatible with Python datetime.datetime objects.
    """
    def __init__(self, iterable, timezone=None): ...

class POSIXlt(Vector):
    """
    R POSIXlt vector for date-time as list structure.
    
    Broken-down time representation storing components
    like year, month, day, hour, minute, second separately.
    """
    def __init__(self, iterable, timezone=None): ...

Usage Examples

from rpy2.robjects.vectors import (IntVector, FloatVector, StrVector, 
                                   BoolVector, ByteVector, ComplexVector,
                                   FactorVector, DataFrame, Matrix, Array)
import rpy2.robjects as ro

# Create vectors from Python data
int_vec = IntVector([1, 2, 3, 4, 5])
float_vec = FloatVector([1.1, 2.2, 3.3, 4.4, 5.5])
str_vec = StrVector(['a', 'b', 'c', 'd', 'e'])

# Vector operations
print(len(int_vec))  # 5
print(int_vec[0])    # 1
print(list(int_vec)) # [1, 2, 3, 4, 5]

# Named vectors
int_vec.names = str_vec
print(int_vec.names)  # ['a', 'b', 'c', 'd', 'e']

# Create data frame
df = DataFrame({
    'integers': int_vec,
    'floats': float_vec,
    'strings': str_vec
})

print(df.nrow)     # 5
print(df.ncol)     # 3
print(df.colnames) # ['integers', 'floats', 'strings']

# Access data frame columns
print(df[0])  # integers column
print(df['floats'])  # floats column by name

# Factor vectors for categorical data
from rpy2.robjects.vectors import FactorVector
categories = FactorVector(['low', 'medium', 'high', 'low', 'high'])
print(categories.levels)  # ['high', 'low', 'medium']

# Matrix operations
from rpy2.robjects.vectors import Matrix
import numpy as np

# Create matrix from Python data
matrix_data = Matrix(range(12), nrow=3, ncol=4)
print(matrix_data.nrow)  # 3
print(matrix_data.ncol)  # 4

Install with Tessl CLI

npx tessl i tessl/pypi-rpy2

docs

environment-management.md

high-level-interface.md

index.md

jupyter-integration.md

low-level-interface.md

package-management.md

type-conversion.md

vectors-datatypes.md

tile.json