R 整数拆分与矩阵转换
我想知道是否有可能将每个整数拆分成一组数字,并将其转换为转移矩阵,例如R 整数拆分与矩阵转换,r,string,matrix,R,String,Matrix,我想知道是否有可能将每个整数拆分成一组数字,并将其转换为转移矩阵,例如 data<-c(11,123,142,1423,1234,12) 我的矩阵永远不会超过5x5。下面是我所做的工作,但它真的很乏味 data2<-as.matrix(as.character(data)) for(i in 1:nrow(data2)) { values<-strsplit(data2,"") } values2<-t(sapply(values, '[', 1:max(sapply(
data<-c(11,123,142,1423,1234,12)
我的矩阵永远不会超过5x5。下面是我所做的工作,但它真的很乏味
data2<-as.matrix(as.character(data))
for(i in 1:nrow(data2)) {
values<-strsplit(data2,"")
}
values2<-t(sapply(values, '[', 1:max(sapply(values, length))))
values2[is.na(values2)]<-0
values3<-apply(values2,2,as.numeric)
from1to1<-0
from1to2<-0
from1to3<-0
from1to4<-0
from1to5<-0
from2to1<-0
from2to2<-0
from2to3<-0
from2to4<-0
...
from5to4<-0
from5to5<-0
for(i in 1:nrow(values3)){
for(j in 1:(ncol(values3)-1))
if (((values3[i,j]==1)&(values3[i,j+1]==1))){
from1to1<-from1to1 + 1
}else{
if (((values3[i,j]==1)&(values3[i,j+1]==2))){
from1to2<-from1to2 + 1
}else{
if (((values3[i,j]==1)&(values3[i,j+1]==3))){
from1to3<-from1to3 + 1
}else{
if (((values3[i,j]==1)&(values3[i,j+1]==4))){
from1to4<-from1to4 + 1
}else{
if (((values3[i,j]==1)&(values3[i,j+1]==5))){
from1to5<-from1to5 + 1
}else{
if (((values3[i,j]==1)&(values3[i,j+1]==1))){
from1to1<-from1to1 + 1
}else{.....continues through all other from2to1...from5to5``
data2这里有一个选项,在这里以管道方式显示,以便易于遵循:
library(magrittr) # for the pipe
# initialize a matrix of zeros
mat <- matrix(0, 5, 5)
# split each element into individual digits
strsplit(as.character(data), '') %>%
# turn list elements back to integers
lapply(as.integer) %>%
# make a 2 column matrix of each digit paired with the previous digit
lapply(function(x){matrix(c(x[-length(x)], x[-1]), ncol = 2)}) %>%
# reduce list to a single 2-column matrix
do.call(rbind, .) %>%
# for each row, add 1 to the element of mat they subset
apply(1, function(x){mat[x[1], x[2]] <<- mat[x[1], x[2]] + 1; x})
# output is the transpose of the matrix; the real results are stored in mat
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
## [1,] 1 1 2 1 4 1 4 2 1 2 3 1
## [2,] 1 2 3 4 2 4 2 3 2 3 4 2
mat
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 3 0 2 0
## [2,] 0 0 3 0 0
## [3,] 0 0 0 1 0
## [4,] 0 2 0 0 0
## [5,] 0 0 0 0 0
library(magrittr)#用于管道
#初始化一个零矩阵
材料%
#将列表元素恢复为整数
lapply(作为整数)%>%
#将每个数字与前一个数字配对,形成一个2列矩阵
lappy(函数(x){matrix(c(x[-length(x)],x[-1]),ncol=2}]%>%
#将列表缩减为单个2列矩阵
do.呼叫(rbind),%%>%
#对于每一行,向其子集的元素添加1
应用(1,函数(x){mat[x[1],x[2]]如果我理解正确,这种情况下最常见的方法是构建一个2列“矩阵”,存储要插入到5x5“矩阵”中的值的[row,col]索引。即构建do.call(rbind,lapply(strsplit)(as.character(data),“”),函数(x){v=as.numeric(x);cbind(v[-length(x)],v[-1])})
。然后,请参见?xtabs
,或者更好地将以前的“矩阵”存储为,比如说,tmp
,使用matrix::sparseMatrix(i=tmp[,1],j=tmp[,2],x=1L,dims=c(5,5),dimnames=list(1:5,1:5))
利用稀疏矩阵。您需要反向组合吗?例如,对于12
您希望矩阵填充一次mat[1,2]
还是也需要填充mat[2,1]
?这看起来很有趣,但是,矩阵显示了一个唯一的计数,例如,它显示了1到2的计数,而数据集中有3个1到2的计数。有没有办法显示计数的数量而不仅仅是唯一的计数?喜欢管道,不知道它存在,将来会更频繁地使用它。更新;你真的只需要放弃unique
调用并添加一个,而不是分配它。xtabs
版本自动以这种方式工作,而无需unique
,因为它就是这样做的。
library(magrittr) # for the pipe
# initialize a matrix of zeros
mat <- matrix(0, 5, 5)
# split each element into individual digits
strsplit(as.character(data), '') %>%
# turn list elements back to integers
lapply(as.integer) %>%
# make a 2 column matrix of each digit paired with the previous digit
lapply(function(x){matrix(c(x[-length(x)], x[-1]), ncol = 2)}) %>%
# reduce list to a single 2-column matrix
do.call(rbind, .) %>%
# for each row, add 1 to the element of mat they subset
apply(1, function(x){mat[x[1], x[2]] <<- mat[x[1], x[2]] + 1; x})
# output is the transpose of the matrix; the real results are stored in mat
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
## [1,] 1 1 2 1 4 1 4 2 1 2 3 1
## [2,] 1 2 3 4 2 4 2 3 2 3 4 2
mat
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 3 0 2 0
## [2,] 0 0 3 0 0
## [3,] 0 0 0 1 0
## [4,] 0 2 0 0 0
## [5,] 0 0 0 0 0