当前位置:网站首页>How to change a matrix into a triple in R language (i.e. three columns: row, col, value)
How to change a matrix into a triple in R language (i.e. three columns: row, col, value)
2022-06-27 14:32:00 【Analysis of breeding data】
bing On the search :
How a matrix becomes a triple , stay bing Search results on :
Various C Language , I can't find R The implementation of language .
R The cycle of language writing is very slow , Just use the ready-made bag
Matrix
Matrix Is a very powerful package , It can transform a matrix into a sparse matrix , take 0 Turn into ., Saving resource .
test , Generate 10*10 Matrix , There's a lot of 0, Now it is not 0 The place is transformed into the form of triples .
What is meant by A triple , Even if the matrix is transformed into a three column form , The matrix is two-dimensional , Now extract the line numbers , As the first column , Extract the column number , As the second column , The corresponding values are extracted , As the third column .
such as :
1 2
3 4
- 1.
- 2.
To become a triple is :
1 1 1
1 2 2
2 1 3
2 2 4
- 1.
- 2.
- 3.
- 4.
Test data
# The matrix becomes a triple : R Language Matrix
mm = matrix(sample(c(0,0,2),size = 100,replace = T),10,10)
# Suppose it is a symmetric matrix , Keep only the lower triangle , So the triangle is 0
mm[upper.tri(mm)] = 0
mm
- 1.
- 2.
- 3.
- 4.
- 5.
> mm
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 2 0 0 0 0 0 0 0 0 0
[2,] 0 2 0 0 0 0 0 0 0 0
[3,] 0 2 2 0 0 0 0 0 0 0
[4,] 0 0 0 0 0 0 0 0 0 0
[5,] 0 0 0 0 2 0 0 0 0 0
[6,] 0 0 0 2 2 0 0 0 0 0
[7,] 0 0 0 0 0 0 0 0 0 0
[8,] 2 0 0 0 2 0 0 0 0 0
[9,] 0 0 0 2 0 2 0 0 0 0
[10,] 0 0 0 0 0 0 0 2 2 0
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
Into a sparse matrix :
You can see , Sparse matrix all 0 Have become a dot .
library(Matrix)
smm = Matrix(mm)
smm
- 1.
- 2.
- 3.
> smm
10 x 10 sparse Matrix of class "dtCMatrix"
[1,] 2 . . . . . . . . .
[2,] . 2 . . . . . . . .
[3,] . 2 2 . . . . . . .
[4,] . . . . . . . . . .
[5,] . . . . 2 . . . . .
[6,] . . . 2 2 . . . . .
[7,] . . . . . . . . . .
[8,] 2 . . . 2 . . . . .
[9,] . . . 2 . 2 . . . .
[10,] . . . . . . . 2 2 .
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
Transform a sparse matrix into a triple
# Become triple
re = summary(smm)
sm = as.data.frame(re)
sm
- 1.
- 2.
- 3.
- 4.
result :
> sm
i j x
1 1 1 2
2 8 1 2
3 2 2 2
4 3 2 2
5 3 3 2
6 6 4 2
7 9 4 2
8 5 5 2
9 6 5 2
10 8 5 2
11 9 6 2
12 10 8 2
13 10 9 2
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
This should be a more efficient method .
Complete code :
# The matrix becomes a triple : R Language Matrix
mm = matrix(sample(c(0,0,2),size = 100,replace = T),10,10)
mm
# Suppose it is a symmetric matrix , Keep only the lower triangle , So the triangle is 0
mm[upper.tri(mm)] = 0
mm
library(Matrix)
smm = Matrix(mm)
smm
# Become triple
re = summary(smm)
sm = as.data.frame(re)
sm
library(dplyr)
arrange(sm,i,j)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
边栏推荐
- 巧用redis实现点赞功能,它不比mysql香吗?
- 力扣 第 81 场双周赛
- Acwing game 57
- 原子操作类
- Pytoch learning 2 (CNN)
- 海量数据!秒级分析!Flink+Doris构建实时数仓方案
- [business security-02] business data security test and example of commodity order quantity tampering
- Openssf security plan: SBOM will drive software supply chain security
- 【业务安全-01】业务安全概述及测试流程
- CCID Consulting released the database Market Research Report on key application fields during the "14th five year plan" (attached with download)
猜你喜欢

做一篇人人能搞懂的ThreadLocal(源码)

Acwing game 57
Talk about redis transactions

Design and implementation of reading app based on Web Platform

Tsinghua & Shangtang & Shanghai AI & CUHK proposed Siamese image modeling, which has both linear probing and intensive prediction performance

基于WEB平台的阅读APP设计与实现

enable_ if

Using redis skillfully to realize the like function, isn't it more fragrant than MySQL?

Design and implementation of food recipe and ingredients website based on vue+node+mysql

Redis persistence
随机推荐
【业务安全-04】万能用户名及万能密码实验
AutoCAD - line width setting
Tsinghua & Shangtang & Shanghai AI & CUHK proposed Siamese image modeling, which has both linear probing and intensive prediction performance
海量数据!秒级分析!Flink+Doris构建实时数仓方案
Implementing springboard agent through SSH port forwarding configuration
Array related knowledge
优雅的自定义 ThreadPoolExecutor 线程池
直播app运营模式有哪几种,我们该选择什么样的模式?
跨境电商多商户系统怎么选
外部存储器
阅读别人的代码,是一种怎样的体验
NAACL 2022 | TAMT:通过下游任务无关掩码训练搜索可迁移的BERT子网络
my. INI file configuration
[WUSTCTF2020]girlfriend
Getting to know cloud native security for the first time: the best guarantee in the cloud Era
What if the win system cannot complete the update and is revoking the status change
Pycharm安装与设置
Privacy computing fat offline prediction
Using redis skillfully to realize the like function, isn't it more fragrant than MySQL?
Debug tool