Alexey Didorenko
Alexey Didorenko

Reputation: 3

Splitting a matrix into multiple matrices

There are two matrices:

  1. Matrix with 2 columns: node name and node degree (k1):

  2. Matrix with 1 column: degrees (ms):

I need to split 1st matrix into multiple matrices, where every matrix has nodes of same degree. Then, write matrices to csv-files. But my code is not working. How can i do this correctly?

k1<-read.csv2("VandD.csv", header = FALSE)
fnk1<-as.matrix(k1)
ms<-read.csv2("mas.csv", header = FALSE)
massive<-as.matrix(ms)
wlk<-1
varbl<-1
rtt<-list()
for (wlk in 1:384) {
  rtt<-NULL
  stepen<-massive[wlk]
  for (varbl in 1:2154) {
    if(fnk1[varbl,2]==stepen){
      kapa<-fnk1[varbl,1]
      rtt<-append(rtt,kapa)
    }
  }
  namef<-paste("reslt",stepen,".csv",sep = "")
  write.csv2(rtt, file=namef)
}
k1
                          V1  V2
1   UC7Ucs42FZy3uYzjrqzOIHsw  81
2   UCyWDmyZRjrGHeKF-ofFsT5Q  81
3   UCIZP6nCTyU9VV0zIhY7q1Aw  81
4   UCqk3CdGN_j8IR9z4uBbVPSg  81
5   UCjWzQkWu0l1yAhcBoavokng  81
6   UCRXiA3h1no_PFkb1JCP0yMA  81
7   UC2w9SdXpwq2Uq-MV4W4A8kw  81
8   UCdJqTQJZleoxZFReiyNvn8w  81
9   UC2Qw1dzXDBAZPwS7zm37g8g  81
10  UCTOovOHTf4efJOmGvJBxIQQ  81

ms
      V1
1     81
2     82
3     83
4     84
5     85
6     86
7     87
8     88
9     89
10    90

Upvotes: 0

Views: 350

Answers (2)

akrun
akrun

Reputation: 886938

We can use group_split

library(dplyr)
k1 %>%
   group_split(v2)

Upvotes: 1

ThomasIsCoding
ThomasIsCoding

Reputation: 101024

Seems you need split

split(k1,k1$v2)

Upvotes: 1

Related Questions