如何读取R中缺少末端元素的矩阵? [英] How can I read a matrix with missing end elements in R?

查看:96
本文介绍了如何读取R中缺少末端元素的矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向R传递一个带有矩阵的txt文件,其中行尾省略了零尾(第一个零尾除外,如果有的话).那些缺失值被视为零.

I would like to pass to R a txt file with a matrix, where tailing zeros are omitted from rows (except the first tailing zero, if any). Those missing values considered as zeros.

例如:

8 7 0
5 4 3 2 1
4 8 9

应读取为:

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

这篇关于如何读取R中缺少末端元素的矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆