R

q1:r中讀入csv文件時(shí),讀入所有的列合并為一列

A1:此時(shí)因?yàn)楸4鏁r(shí)是write.table(),,所以讀入直接read.table就行,什么參數(shù)也別加,,,如果不是直接保存的測(cè)試v、文件的話,讀入用read.csv(),參數(shù)要加header = T,sep = ','

r包下載地方:

The downloaded source packages are in
‘/tmp/Rtmp1UrAkM/downloaded_packages’

安裝位置:~/R/x86_64-pc-linux-gnu-library/4.0(服務(wù)器)

library(Matrix)
M <- Matrix(c(0, 0,  0, 2,
              6, 0, -1, 5,
              0, 4,  3, 0,
              0, 0,  5, 0),
            byrow = TRUE, nrow = 4, sparse = TRUE)
rownames(M) <- paste0("r", 1:4)
colnames(M) <- paste0("c", 1:4)
M
# 4 x 4 sparse Matrix of class "dgCMatrix"
#    c1 c2 c3 c4
# r1  .  .  .  2
# r2  6  . -1  5
# r3  .  4  3  .
# r4  .  .  5  .

認(rèn)識(shí)dgcMatrix

str(M)
# Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
# ..@ i       : int [1:7] 1 2 1 2 3 0 1
# ..@ p       : int [1:5] 0 1 2 5 7
# ..@ Dim     : int [1:2] 4 4
# ..@ Dimnames:List of 2
# .. ..$ : chr [1:4] "r1" "r2" "r3" "r4"
# .. ..$ : chr [1:4] "c1" "c2" "c3" "c4"
# ..@ x       : num [1:7] 6 4 -1 3 5 2 5
# ..@ factors : list()

x:矩陣中的數(shù)字,第一列非0的數(shù)字依次寫,接著第二列第三列

M
# 4 x 4 sparse Matrix of class "dgCMatrix"
#    c1 c2 c3 c4
# r1  .  .  .  2
# r2  6  . -1  5
# r3  .  4  3  .
# r4  .  .  5  .

M@x
# [1]  6  4 -1  3  5  2  5

as.numeric(M)[as.numeric(M) != 0]
# [1]  6  4 -1  3  5  2  5

I: 存儲(chǔ)這些非0數(shù)字所在的行數(shù),特殊的,第一行為0

M
# 4 x 4 sparse Matrix of class "dgCMatrix"
#    c1 c2 c3 c4
# r1  .  .  .  2
# r2  6  . -1  5
# r3  .  4  3  .
# r4  .  .  5  .

M@i
# [1] 1 2 1 2 3 0 1

p:If we index the columns such that the first column has index ZERO, then M@p[1] = 0, and M@p[j+2] - M@p[j+1] gives us the number of non-zero elements in column j.

In our example, when j = 2, M@p[2+2] - M@p[2+1] = 5 - 2 = 3, so there are 3 non-zero elements in column index 2 (i.e. the third column).其實(shí)就是每列非0的個(gè)數(shù),第一個(gè)是0,最后一個(gè)是非0的個(gè)數(shù),中間依次是前面n列的非0的個(gè)數(shù)
比如這個(gè)M中的p的形成
第一個(gè)數(shù)字是0 ,最后一個(gè)數(shù)字是矩陣中非0 的個(gè)數(shù),也就是7,那么第二個(gè)數(shù)字就是第一列中非0的個(gè)數(shù)(1),第三個(gè)數(shù)字就是第一列的非0數(shù)加第二列的非0數(shù),第四個(gè)數(shù)字是第三列的非0數(shù)加前面所有列的非0數(shù),然后依次加到最后一個(gè)

M
# 4 x 4 sparse Matrix of class "dgCMatrix"
#    c1 c2 c3 c4
# r1  .  .  .  2
# r2  6  . -1  5
# r3  .  4  3  .
# r4  .  .  5  .

M@p
# [1] 0 1 2 5 7

dims:行數(shù),列數(shù)

dimnames:行名,列名

library(Matrix)
m <- Matrix(c(0,0,0,20,5,
              2,5,8,0,0,
              0,0,0,0,8),
            byrow = TRUE,nrow = 3,sparse = TRUE)
rownames(m) <- paste0('r',1:3)
colnames(m) <- paste0('c',1:5)
m
3 x 5 sparse Matrix of class #"dgCMatrix"
#   c1 c2 c3 c4 c5
#r1  .  .  . 20  5
#r2  2  5  8  .  .
#r3  .  .  .  .  8
str(m)

#Formal class 'dgCMatrix'
 #[package "Matrix"] with 6 slots
#  ..@ i       : int [1:6] 1 1 1 0 0 2
#  ..@ p       : int [1:6] 0 1 2 3 4 6
#  ..@ Dim     : int [1:2] 3 5
 # ..@ Dimnames:List of 2
#  .. ..$ : chr [1:3] "r1" "r2" "r3"
#  .. ..$ : chr [1:5] "c1" "c2" "c3" #"c4" ...
#  ..@ x       : num [1:6] 2 5 8 20 5 8
#  ..@ factors : list()
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容