[英]How to create square data frame (or matrix) in R
Let's say I have some IDs 1,3 and 5, how would I create data frame (or matrix) which has those ID's as column/row names, so looking like this: 假设我有一些ID 1,3和5,我将如何创建将那些ID作为列/行名称的数据框(或矩阵),如下所示:
IDs
IDS 1 3 5
1 NA NA NA
3 NA NA NA
5 NA NA NA
Just a note, those IDs could be any large number, I just took that as example. 请注意,这些ID可以是任何数字,我只是以它为例。 Here below is another example, what I'm aiming that. 下面是另一个示例,我的目标是。 I found this example of data but it was already in .rdata format so I have no clue how to re-create it. 我找到了这个数据示例,但它已经是.rdata格式,因此我不知道如何重新创建它。
Terms
Terms applications code computing
analysis 0 1 0
applications 9 0 0
code 0 9 0
You could do 你可以做
matrix(nr = 3, nc = 3, dimnames = rep(list(IDS = c(1,3,5)), 2))
# IDS
# IDS 1 3 5
# 1 NA NA NA
# 3 NA NA NA
# 5 NA NA NA
Or if you're golfing 或者如果您正在打高尔夫球
matrix(, 3, 3, dimnames = rep(list(IDS = c(1,3,5)), 2))
If you want to have a data frame, you could do 如果您想要一个数据框,则可以
tmp <- rep(NA, 3)
data.frame(ID1 = tmp, ID2 = tmp, ID3 = tmp, row.names = paste('Row', 1:3))
# ID1 ID2 ID3
# Row 1 NA NA NA
# Row 2 NA NA NA
# Row 3 NA NA NA
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.