R,我如何将单列向量转换为NxM矩阵?

时间:2016-10-24 21:43:43

标签: r matrix

我有以下单列矩阵,由值列(值),X坐标列和Y坐标列组成。

X       Y       value
-       -       -
100     100     0.216
6100    100     0.00109
12100   100     0.00853
18100   100     0.0197
24100   100     0.000038
100     6100    0.506
6100    6100    0.00231
12100   6100    0.0125
18100   6100    0.0306
24100   6100    3.36E-005
100     12100   0.506
6100    12100   0.00231
12100   12100   0.0159
18100   12100   0.0329
24100   12100   3.93E-007
100     18100   0.506
6100    18100   0.00292
12100   18100   0.0163
18100   18100   0.000243
24100   18100   8.20E-007
100     24100   0.506
6100    24100   0.00322
12100   24100   0.000182
18100   24100   0.00019
24100   24100   5.38E-006

我想把它转换成这个矩阵:

X\Y       100   6100    12100   18100   24100
100     0.216   0.00109 0.00853 0.0197  0.000038
6100    0.506   0.00231 0.0125  0.0306  0.0000336
12100   0.506   0.00231 0.0159  0.0329  0.000000393
18100   0.506   0.00292 0.0163  0.000243    0.00000082
24100   0.506   0.00322 0.000182    0.00019 0.00000538

我怎么能在R中这样做? 有没有这样做的功能?

5 个答案:

答案 0 :(得分:1)

你可以这样做:

library(reshape2)
dcast(df, Y ~ X)

#       Y   100    6100    12100    18100    24100
# 1   100 0.216 0.00109 0.008530 0.019700 3.80e-05
# 2  6100 0.506 0.00231 0.012500 0.030600 3.36e-05
# 3 12100 0.506 0.00231 0.015900 0.032900 3.93e-07
# 4 18100 0.506 0.00292 0.016300 0.000243 8.20e-07
# 5 24100 0.506 0.00322 0.000182 0.000190 5.38e-06

您是否希望值100,6100,...成为您可以执行的行的名称:

final <- dcast(df, Y ~ X)
rownames(final) <- final[, 1]
final[, 1] <- NULL

# final
#         100    6100    12100    18100    24100
# 100   0.216 0.00109 0.008530 0.019700 3.80e-05
# 6100  0.506 0.00231 0.012500 0.030600 3.36e-05
# 12100 0.506 0.00231 0.015900 0.032900 3.93e-07
# 18100 0.506 0.00292 0.016300 0.000243 8.20e-07
# 24100 0.506 0.00322 0.000182 0.000190 5.38e-06

以下是数据:

df <- structure(list(X = c(100L, 6100L, 12100L, 18100L, 24100L, 100L, 
6100L, 12100L, 18100L, 24100L, 100L, 6100L, 12100L, 18100L, 24100L, 
100L, 6100L, 12100L, 18100L, 24100L, 100L, 6100L, 12100L, 18100L, 
24100L), Y = c(100L, 100L, 100L, 100L, 100L, 6100L, 6100L, 6100L, 
6100L, 6100L, 12100L, 12100L, 12100L, 12100L, 12100L, 18100L, 
18100L, 18100L, 18100L, 18100L, 24100L, 24100L, 24100L, 24100L, 
24100L), value = c(0.216, 0.00109, 0.00853, 0.0197, 3.8e-05, 
0.506, 0.00231, 0.0125, 0.0306, 3.36e-05, 0.506, 0.00231, 0.0159, 
0.0329, 3.93e-07, 0.506, 0.00292, 0.0163, 0.000243, 8.2e-07, 
0.506, 0.00322, 0.000182, 0.00019, 5.38e-06)), .Names = c("X", 
"Y", "value"), class = "data.frame", row.names = c(NA, -25L))

答案 1 :(得分:1)

只需在基础R中使用xtabs

xtabs(value~Y+X, df)

#       X
#Y               100        6100       12100       18100       24100
#  100   0.216000000 0.001090000 0.008530000 0.019700000 0.000038000
#  6100  0.506000000 0.002310000 0.012500000 0.030600000 0.000033600
#  12100 0.506000000 0.002310000 0.015900000 0.032900000 0.000000393
#  18100 0.506000000 0.002920000 0.016300000 0.000243000 0.000000820
#  24100 0.506000000 0.003220000 0.000182000 0.000190000 0.000005380

答案 2 :(得分:0)

使用data.tabledcast.data.table

library(data.table)
dcast(data = setDT(dx),formula = X~Y)
#        X      100     6100    12100    18100    24100
# 1:   100 0.216000 5.06e-01 5.06e-01 5.06e-01 5.06e-01
# 2:  6100 0.001090 2.31e-03 2.31e-03 2.92e-03 3.22e-03
# 3: 12100 0.008530 1.25e-02 1.59e-02 1.63e-02 1.82e-04
# 4: 18100 0.019700 3.06e-02 3.29e-02 2.43e-04 1.90e-04
# 5: 24100 0.000038 3.36e-05 3.93e-07 8.20e-07 5.38e-06

答案 3 :(得分:0)

您想用稀疏三元组表示法创建24100x24100矩阵(每行代表矩阵中的非零单元格)吗?这可以使用Matrix库完成:

sp <- read.table(text="100     100     0.216
6100    100     0.00109
12100   100     0.00853
18100   100     0.0197
24100   100     0.000038
100     6100    0.506
6100    6100    0.00231
12100   6100    0.0125
18100   6100    0.0306
24100   6100    3.36E-005
100     12100   0.506
6100    12100   0.00231
12100   12100   0.0159
18100   12100   0.0329
24100   12100   3.93E-007
100     18100   0.506
6100    18100   0.00292
12100   18100   0.0163
18100   18100   0.000243
24100   18100   8.20E-007
100     24100   0.506
6100    24100   0.00322
12100   24100   0.000182
18100   24100   0.00019
24100   24100   5.38E-006")

library(Matrix)
spMat <- spMatrix(24100, 24100, sp[,1], sp[,2], sp[,3])

这是一个稀疏矩阵,意味着它只为非零单元分配内存:

object.size(spMat)
# 1872 bytes

答案 4 :(得分:0)

如果您想避免使用数据框,可以使用slam包中的dgTMatrixSparseM中的sparseMatrix类转换为三联形式的矩阵as.matrix函数。

从那里,您可以使用library(slam) mat = simple_triplet_matrix(c(1,3,4),c(2,4,5),c(2,4,2)) as.matrix(mat)

转换为矩阵

例如:

var sqlSearch = "SELECT * FROM Profiles WHERE BodyType='"+bodyType+ "' OR   Education='" + education + "' OR Eyes ='"+eyes+"' OR Hairs ='"+ hairs + "' OR Heights ='"+heights+"' OR Religion ='" + religion+"'";

sql = "INSERT INTO Profiles (MemberID, Description, Religion, HairColor, Eye, Education, Height, BodyType) VALUES ('" + Session("MemberID")+"', '"+description+"', '"+religion +"', '"+hairs+"', '"+eyes+"', '"+education+"', '"+heights+"', '"+bodyType+"')";