R语言 把矩阵或数据帧转换为稀疏矩阵

R语言 把矩阵或数据帧转换为稀疏矩阵

稀疏矩阵是面向列的格式,它们主要包含空值。稀疏矩阵中非空值的元素是按升序排列的。在这篇文章中,我们将在R编程语言中把矩阵和数据帧转换为稀疏矩阵。

将矩阵转换为稀疏矩阵

正如我们所知,R编程语言中的矩阵是以二维布局排列的对象或元素集合。我们可以使用 matrix() 函数在R语言中构建一个矩阵。

我们要做的第一步是使用 install.packages(“Matrix “)安装 Matrix包,然后使用R中的 库函数 加载该包。矩阵生成后,使用as()创建一个等效的稀疏矩阵。

语法:

sparsematrix <- as(BaseMatrix, "sparseMatrix")

参数:

  • sparsematrix : 这是我们的样本稀疏矩阵,将从我们的基础矩阵转换而来。
  • BaseMatrix : 这是我们的R矩阵样本。
  • sparseMatrix : 这是在as()函数中指定的类别,用于将基础R矩阵转换成稀疏格式。

例子: 在R中将矩阵转换为稀疏矩阵

# loading the Matrix package
library(Matrix)
  
# Constructing a base R matrix 
set.seed(0)
nrows <- 6L
ncols <- 8L
values <- sample(x = c(0,1,2,3), prob = c(0.6,0.2,0.4,0.8), 
                 size = nrows*ncols, replace = TRUE)
  
BaseMatrix <- matrix(values, nrow = nrows)
BaseMatrix
  
# For converting base matrix to sparse matrix
sparsematrix <- as(BaseMatrix, "sparseMatrix")
sparsematrix
R

输出:

在R中把矩阵或数据帧转换为稀疏矩阵

将数据框架转换为稀疏矩阵

我们知道,数据框架是一个表格或类似于二维数组的结构,既有行又有列,是最常见的存储数据的方式。我们将通过使用R中的 sparseMatrix() 函数将数据框架转换为稀疏矩阵。

语法: sparseMatrix(i = ep, j = ep, p, x, dims, dimnames, symmetric = FALSE, triangular = FALSE, index1 = TRUE, repr = "C", giveCsparse =(repr == "C"), check = "TRUE", use.last.ij = FALSE)

参数:

  • i, j : 这些是相同长度的整数,指定矩阵的行和列索引的位置。
  • p : 这些是指针的整数向量,在基于零的行和列索引中,每一列或行都有一个。
  • x : 这些是矩阵条目中使用的可选值。
  • dims : 这些是非负的整数向量。
  • dimnames : 这些是 “dimnames “的可选列表。
  • symmetric : 这是一个逻辑变量。如果它被指定为 “true”,那么生成的矩阵应该是对称的,否则就是 “false”。
  • triangular : 这也是一个逻辑变量,如果指定为true,那么生成的矩阵应该是三角形的,否则为false。
  • index1 : 这是一个逻辑标量变量。如果它为真,那么行和列的计数从1开始;如果它为假,那么行和列的计数从0开始。
  • repr : 这些是字符串,用于指定结果的稀疏表示。
  • giveCsparse : 这是一个逻辑变量,表示结果矩阵是Csparse还是Tsparse。
  • check : 这是一个逻辑变量,表示是否进行了有效性检查。
  • use.last.ij : 这也是一个逻辑变量,表示在出现重复的数据对时,只使用最后一个。

例子: 在R中把数据框架转换为稀疏矩阵

library(Matrix)
  
# Creating a table of buyers
buyer <- data.frame(Buyers = c("Robert", "Stewart", "Kristen", 
                               "Joe", "Kriti", "Rafel"))
buyer
  
# Creating a table of cars
car <- data.frame(Cars = c("Maruti", "Sedan", "SUV", "Baleno", 
                           "Hyundai", "BMW","Audi"))
car
  
# Creating a table of orders: (Buyers, cars, units)
# triplets
order <- data.frame(Buyers = c("Robert", "Robert", "Stewart", 
                               "Stewart", "Kristen", "Kristen",
                               "Joe", "Kriti", "Joe"),
                    Cars = c("Maruti", "Maruti", "BMW", "BMW", 
                             "Audi", "Audi", "Maruti", "Audi", 
                             "Sedan"))
  
# Insert the RowIndex column, identifying 
# the row index to assign each buyer
orderRowIndex <- match(orderBuyers, buyerBuyers)
  
# Insert the ColIndex column, identifying 
# the column index to assign each car
orderColIndex <- match(orderCars, carCars)
  
# Now inspect
order
  
# Creating a basic sparse matrix where element
# (i,j) is true if buyer i bought
# car j and false, otherwise
msparse1 <- sparseMatrix( i = orderRowIndex, j = orderColIndex)
msparse1
  
# Creating another sparse matrix to make sure 
# every buyer and every car appears in our matrix
# by setting the dimensions explicitly
msparse2 <- sparseMatrix( i = orderRowIndex, j = orderColIndex, 
                         dims = c(nrow(buyer), nrow(car)), 
                         dimnames = list(buyerBuyers, carCars))
msparse2
  
# Creating another sparse matrix indicating number 
# of times buyer i bought car j
msparse3 <- sparseMatrix( i = orderRowIndex, j = orderColIndex, x = 1L, 
                         dims = c(nrow(buyer), nrow(car)),
                         dimnames = list(buyerBuyers, carCars))
msparse3
R

输出:

在R中把矩阵或数据帧转换为稀疏矩阵

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册