如何将具有可变列的文件读入R中的矩阵?

时间:2022-05-28 14:58:53

I have a data file of the following format:

我有一个以下格式的数据文件:

A1 B1 C1 D1 E1 F1 G1 H1 I1 J1 K1 L1
   a1 b1 c1 d1 e1 f1 g1 h1 i1 j1 k1
A2 B2 C2 D2 E2 F2 G2 H2 I2 J2 K2 L2 M2 N2 O2 P2 Q2 R2
   a2 b2 c2 d2 e2 f2 g2 h2 i2 j2 k2 l2 m2 n2 o2 p2 q2
A3 B3 C3 D3 E3 F3
   a3 b3 c3 d3 e3

Here, Ai's, Bi's, Ci's and so on, are strings and ai's, bi's, ci's etc. are real numbers. The file is large(arnd 450 rows), and the no of columns for rows vary greatly. I want to read this file to matrix in the same format as above. The following command

在这里,Ai的,Bi的,Ci的等等,是字符串和ai的,bi的,ci的等等是实数。文件很大(arnd 450行),行的列数差别很大。我想以与上面相同的格式将此文件读取到矩阵。以下命令

mat=read.csv(file_names, header=F, sep="|", na.strings="", as.is=T)

doesn't help. The number of columns of the resulting matrix is not equal to the maximum length of row in the matrix, and the data is saved in the following fashion:

没有帮助。结果矩阵的列数不等于矩阵中行的最大长度,数据以下列方式保存:

A1 B1 C1 D1 E1 F1 G1 H1 I1
J1 K1 L1 NA NA NA NA NA NA
   a1 b1 c1 d1 e1 f1 g1 h1
i1 j1 k1 NA NA NA NA NA NA
A2 B2 C2 D2 E2 F2 G2 H2 I2
J2 K2 L2 M2 N2 O2 NA NA NA
   a2 b2 c2 d2 e2 f2 g2 h2
i2 j2 k2 l2 m2 n2 NA NA NA
A3 B3 C3 D3 E3 F3
   a3 b3 c3 d3 e3

Can someone please help me out? I'm really stuck :(

有人可以帮帮我吗?我真的被卡住了:(

Also, once I have read the matrix in the desired manner, how can I possibly get the length of each row?

此外,一旦我以所需的方式读取矩阵,我怎么可能得到每行的长度?

Thank you

谢谢

1 个解决方案

#1


1  

Are you looking for this?

你在找这个吗?

mat<-read.csv("filename.csv", header=F, sep=",", na.strings="", as.is=T)
mat<-as.matrix(mat)

       V1   V2   V3   V4   V5   V6   V7   V8   V9   V10  V11  V12  V13  V14  V15  V16  V17  V18 
[1,] "A1" "B1" "C1" "D1" "E1" "F1" "G1" "H1" "I1" "J1" "K1" "L1" NA   NA   NA   NA   NA   NA  
[2,] NA   "a1" "b1" "c1" "d1" "e1" "f1" "g1" "h1" "i1" "j1" "k1" NA   NA   NA   NA   NA   NA  
[3,] "A2" "B2" "C2" "D2" "E2" "F2" "G2" "H2" "I2" "J2" "K2" "L2" "M2" "N2" "O2" "P2" "Q2" "R2"
[4,] NA   "a2" "b2" "c2" "d2" "e2" "f2" "g2" "h2" "i2" "j2" "k2" "l2" "m2" "n2" "o2" "p2" "q2"
[5,] "A3" "B3" "C3" "D3" "E3" "F3" NA   NA   NA   NA   NA   NA   NA   NA   NA   NA   NA   NA  
[6,] NA   "a3" "b3" "c3" "d3" "e3" NA   NA   NA   NA   NA   NA   NA   NA   NA   NA   NA   NA  

#1


1  

Are you looking for this?

你在找这个吗?

mat<-read.csv("filename.csv", header=F, sep=",", na.strings="", as.is=T)
mat<-as.matrix(mat)

       V1   V2   V3   V4   V5   V6   V7   V8   V9   V10  V11  V12  V13  V14  V15  V16  V17  V18 
[1,] "A1" "B1" "C1" "D1" "E1" "F1" "G1" "H1" "I1" "J1" "K1" "L1" NA   NA   NA   NA   NA   NA  
[2,] NA   "a1" "b1" "c1" "d1" "e1" "f1" "g1" "h1" "i1" "j1" "k1" NA   NA   NA   NA   NA   NA  
[3,] "A2" "B2" "C2" "D2" "E2" "F2" "G2" "H2" "I2" "J2" "K2" "L2" "M2" "N2" "O2" "P2" "Q2" "R2"
[4,] NA   "a2" "b2" "c2" "d2" "e2" "f2" "g2" "h2" "i2" "j2" "k2" "l2" "m2" "n2" "o2" "p2" "q2"
[5,] "A3" "B3" "C3" "D3" "E3" "F3" NA   NA   NA   NA   NA   NA   NA   NA   NA   NA   NA   NA  
[6,] NA   "a3" "b3" "c3" "d3" "e3" NA   NA   NA   NA   NA   NA   NA   NA   NA   NA   NA   NA