如何创建复制此逻辑的data.frame:
row1 = c (1,0,0,0,0)
row2 = c (0,1,0,0,0)
row3 = c (0,0,1,0,0)
row4 = c (0,0,0,1,0)
row5 = c (0,0,0,0,1)
所以我可以通过某种方式设置二进制参数的总数
答案 0 :(得分:1)
我们可以创建一个0的矩阵并将对角线分配给1
nr <- 5
nc <- 5
df <- as.data.frame(matrix(0, nr, nc))
diag(df) <- 1
df
# V1 V2 V3 V4 V5
#1 1 0 0 0 0
#2 0 1 0 0 0
#3 0 0 1 0 0
#4 0 0 0 1 0
#5 0 0 0 0 1