开发者

How can I read a matrix with missing end elements in R?

开发者 https://www.devze.com 2023-01-27 02:13 出处:网络
I would like to pass to R a txt file with a matrix, where tailing zeros are o开发者_如何学JAVAmitted from rows (except the first tailing zero, if any). Those missing values considered as zeros.

I would like to pass to R a txt file with a matrix, where tailing zeros are o开发者_如何学JAVAmitted from rows (except the first tailing zero, if any). Those missing values considered as zeros.

for example:

8 7 0
5 4 3 2 1
4 8 9

should be read as:

8 7 0 0 0
5 4 3 2 1
4 8 9 0 0

The max row size (i.e. the number of matrix columns) is unknown prior to reading the matrix.


d <- as.matrix(read.table(filename, fill=T))
d[is.na(d)] <- 0
0

精彩评论

暂无评论...
验证码 换一张
取 消