我有一个10x1的字符矩阵(例如e212m)。
> print(e212m)
[,1]
[1,] "0000000000000111111000000000"
[2,] "0000000000000111111100000000"
[3,] "0000000000001111111100000000"
[4,] "0000000000001111111100000000"
[5,] "0000000000011100111100000000"
[6,] "0000000000011111111100000000"
[7,] "0000000000011111111100000000"
[8,] "0000000000011111111100000000"
[9,] "0000000000001111111000000000"
[10,] "0000000000000011111000000000"
> dim(e212m)
[1] 10 1
> typeof(e212m)
[1] "character"
我想将任何行的每个字符转换为整数。但是不喜欢
"0000000000000111111000000000"(string/character) to integer = 0000000000000111111000000000
我希望将每个字符都更改为digit.eg
"0" "0" "1" "1" to number 0 0 1 1.
这样最后我可以得到10x29的整数矩阵。PS:我是R的新手。欢迎执行上述任务的直接命令。
x<-"0000000000000111111000000000"
y<-as.numeric(strsplit(x,split='')[[1]])
将返回
y
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0
如果您将矩阵命名为m,请使用:
m2<-apply(m,1,function(x){as.numeric(strsplit(x,split='')[[1]])})
m2<-t(m2)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句