简体   繁体   English

在比较 R 中的列和行后用值填充空矩阵

[英]Populate an empty matrix with values after comparing column and row in R

Can I ask you how do I with the R language compare a table (attributes: starting machine name, target machine name and incremental id) and an empty matrix (where we have the target machine name in the columns and the starting machine name in the rows ).请问我如何用 R 语言比较一个表(属性:起始机器名、目标机器名和增量 id)和一个空矩阵(我们在列中有目标机器名,在列中有起始机器名)行)。 If there is a match between the start and finish then I want to compile with the incremental id value within the array.如果开始和结束之间存在匹配项,那么我想使用数组中的增量 id 值进行编译。

enter image description here在此处输入图像描述

enter image description here在此处输入图像描述

I expect that inside the array is incremental id我希望数组内部是增量 id

It would be nice if you can provide a reproducible example ( https://reprex.tidyverse.org/ ).如果您能提供一个可重现的示例 ( https://reprex.tidyverse.org/ ),那就太好了。 Based on your images, I assumed that your dataset look like this one:根据您的图像,我假设您的数据集如下所示:

machine_table <- data.frame(
  incremental_id = rep(1:2, times=5),
  start_machine = paste0("A", 1:5),
  target_machine = factor(c("A1", "A2"), levels = paste0("A", 1:5))
)

machine_table

To create your matrix you have multiple possibilities, here you will first filter the row with a matching start and target machine before pivoting and converting to a matrix.要创建您的矩阵,您有多种可能性,在这里您将首先使用匹配的起始机器和目标机器过滤行,然后再旋转并转换为矩阵。

First, convert the machine columns to factors as we already know them.首先,将机器列转换为我们已知的因子。

machine_table$start_machine <-  factor(machine_table$start_machine, levels = paste0("A", 1:5))
machine_table$target_machine <- factor(machine_table$target_machine, levels = paste0("A", 1:5))

Then select only the rows where the machines match and pivot the pivot and expand with unmatched machine names.然后 select 只有机器匹配的行和 pivot pivot 并用不匹配的机器名称展开。 You can use machine_table instead of match in pivot_wider to do the same operation on the whole dataset.您可以使用machine_table而不是pivot_wider中的match对整个数据集执行相同的操作。

match <- machine_table[machine_table$start_machine == machine_table$target_machine, ]
match_table <- tidyr::pivot_wider(match, 
                   id_cols = "start_machine",  id_expand = T,
                   names_from = "target_machine", names_expand = T,
                   values_from =  "incremental_id")

After that you only need to convert this table to a matrix, and renames the rows之后你只需要将这个表转换为矩阵,并重命名行

machine_matrix <- as.matrix(match_table[, -1])
row.names(machine_matrix) <- match_table$start_machine
machine_matrix

If you have multiple value for one pair of machine you can specify an aggregating function like min, max, sd, etc. as values_fn argument in pivot_wider .如果一对机器有多个值,则可以指定聚合 function,如 min、max、sd 等,作为values_fn中的pivot_wider参数。

EDIT: Based on you comment, here is an approach with a for loop, looping through all the rows and columns of the matrix and row of the dataframe.编辑:根据您的评论,这是一种带有 for 循环的方法,遍历矩阵的所有行和列以及 dataframe 的行。

for (i in seq(nrow(tabellaMatrice))) {
  for (j in seq(ncol(tabellaMatrice))) {
    for (r in seq(nrow(tab2))) {
      ifelse(tab2$Da[r]==rownames(tabellaMatrice)[i] & tab2$A[r]==colnames(tabellaMatrice)[j],
             tabellaMatrice[i,j] <- tab2$`Numero incrementale`[r],
             NA
      )
    }
  }
}

If there is multiple values for the same machine pair, the last value in the dataframe will erase the other ones.如果同一机器对有多个值,则 dataframe 中的最后一个值将擦除其他值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM