0
Follow
0
View

Matrix Creation Automation

cqwrmjld 注册会员
2023-01-24 21:58

If the numbers are characters a one-liner can do it.

x <- scan(text='01202
10000
22110
22210
22222
', what = character())
x
#> [1] "01202" "10000" "22110" "22210" "22222"

t(sapply(strsplit(x, ""), as.numeric))
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    0    1    2    0    2
#> [2,]    1    0    0    0    0
#> [3,]    2    2    1    1    0
#> [4,]    2    2    2    1    0
#> [5,]    2    2    2    2    2

Created on 2022-10-02 with reprex v2.0.2

daiyao225 注册会员
2023-01-24 21:58

Using read.fwf from base R

as.matrix(read.fwf(textConnection(str1), widths = rep(1, 5) ))
     V1 V2 V3 V4 V5
[1,]  0  1  2  0  2
[2,]  1  0  0  0  0
[3,]  2  2  1  1  0
[4,]  2  2  2  1  0
[5,]  2  2  2  2  2

data

str1 <- "01202
10000
22110
22210
22222"

About the Author

Question Info

Publish Time
2023-01-24 21:58
Update Time
2023-01-24 21:58