我有一个矩阵,我想转换成一个二进制输出(0对1).要转换的矩阵包含四行排名(1到4):

mat1.data <- c(4,   3,  3,  3,  3,  2,  2,  1,  1,  1,
               3,   4,  2,  4,  2,  3,  1,  3,  3,  2,
               2,   2,  4,  1,  1,  1,  4,  4,  2,  4,
               1,   1,  1,  2,  4,  4,  3,  2,  4,  3)
mat1 <- matrix(mat1.data,nrow=4,ncol=10,byrow=TRUE)
mat1
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    4    3    3    3    3    2    2    1    1     1
[2,]    3    4    2    4    2    3    1    3    3     2
[3,]    2    2    4    1    1    1    4    4    2     4
[4,]    1    1    1    2    4    4    3    2    4     3

在二进制输出矩阵中,当特定排名为真时,每个行条目显示1,否则显示0.转换过程从排名1开始,以排名4结束,因此原始矩阵的每一行应在输出矩阵中产生10×4=40个条目.例如,对于排名4 3 3 3 3 2 2 1 1 1的第一行,输出应为

0   0   0   0   0   0   0   1   1   1
0   0   0   0   0   1   1   0   0   0
0   1   1   1   1   0   0   0   0   0
1   0   0   0   0   0   0   0   0   0

继续此过程,所有四行排名的预期输出应如下所示:

0   0   0   0   0   0   0   1   1   1 #first row of rankings starts
0   0   0   0   0   1   1   0   0   0
0   1   1   1   1   0   0   0   0   0
1   0   0   0   0   0   0   0   0   0 #first row of rankings ends
0   0   0   0   0   0   1   0   0   0 #second row of rankings starts
0   0   1   0   1   0   0   0   0   1
1   0   0   0   0   1   0   1   1   0
0   1   0   1   0   0   0   0   0   0 #second row of rankings ends
0   0   0   1   1   1   0   0   0   0 #third row of rankings starts
1   1   0   0   0   0   0   0   1   0
0   0   0   0   0   0   0   0   0   0
0   0   1   0   0   0   1   1   0   1 #third row of rankings ends
1   1   1   0   0   0   0   0   0   0 #fourth row of rankings starts
0   0   0   1   0   0   0   1   0   0
0   0   0   0   0   0   1   0   0   1
0   0   0   0   1   1   0   0   1   0 #fourth row of rankings ends

我如何做到这一点?我有一个更大的数据集,所以更有效的方法是首选,但任何帮助将不胜感激!

推荐答案

matrix(unlist(lapply(mat1, \(i) replace(numeric(4), i, 1))), ncol = ncol(mat1))
#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# [1,]    0    0    0    0    0    0    0    1    1     1
# [2,]    0    0    0    0    0    1    1    0    0     0
# [3,]    0    1    1    1    1    0    0    0    0     0
# [4,]    1    0    0    0    0    0    0    0    0     0
# [5,]    0    0    0    0    0    0    1    0    0     0
# [6,]    0    0    1    0    1    0    0    0    0     1
# [7,]    1    0    0    0    0    1    0    1    1     0
# [8,]    0    1    0    1    0    0    0    0    0     0
# [9,]    0    0    0    1    1    1    0    0    0     0
#[10,]    1    1    0    0    0    0    0    0    1     0
#[11,]    0    0    0    0    0    0    0    0    0     0
#[12,]    0    0    1    0    0    0    1    1    0     1
#[13,]    1    1    1    0    0    0    0    0    0     0
#[14,]    0    0    0    1    0    0    0    1    0     0
#[15,]    0    0    0    0    0    0    1    0    0     1
#[16,]    0    0    0    0    1    1    0    0    1     0

它需要3个步骤,管道语法可能看起来更清晰:

lapply(mat1, \(i) replace(numeric(4), i, 1)) |>  ## each value to binary vector
 unlist() |>  ## collapse
  matrix(ncol = ncol(mat1))  ## reshape

嗯...为什么我需要匿名函数\(i)?可以通过replacelapply.

lapply(mat1, replace, x = numeric(4), values = 1) |>
 unlist() |>
  matrix(ncol = ncol(mat1))

R相关问答推荐

如何将y轴设置为在ggplot 2中x=0处与x轴相交?

如何删除字符串中重复的字符序列?

按条件计算观察次数

如果窗口在CLARME或集团之外,则有条件领先/滞后滚动总和返回NA

从具有随机模式的字符串中提取值

在R底座中更改白天和夜晚的背景 colored颜色

找出疾病消失的受试者

矩阵%*%矩阵中的错误:需要数字/复杂矩阵/向量参数

使用sf或terra的LINESTRAING的累积长度

使用across,starts_with和ifelse语句变更多个变量

在ggplot中为不同几何体使用不同的 colored颜色 比例

如何同时从多个列表中获取名字?

如何通过ggplot2添加短轴和删除长轴?

更新R中的数据表(使用data.table)

计算Mean by分组和绑定到R中的数据集

对R中的列表列执行ROW Mean操作

构建一个6/49彩票模拟系统

ggplot斜体轴刻度标签中的单个字符-以前的帖子建议不工作

使用ggplot2绘制具有边缘分布的坡度图

使用LAG和dplyr执行计算,以便按行和按组迭代