【问题标题】:Calculate intersection of labels in matrix计算矩阵中标签的交集
【发布时间】:2022-06-11 00:03:06
【问题描述】:

我想构建一个由指标值组成的矩阵,如果标签有共同的元素,则取值 1。 最终,我想创建一个报告两个元素是否匹配的数据库

N <- 100
combinations <- c(outer(letters[1:5], letters[1:5], paste0))

set.seed(1)
data <- data.frame(
  c_1 = sample(x=pseudo_lastnames,size = N,replace = T),
  c_2 = sample(x=c(rep(NA,N/4),combinations),size = N,replace = T),
  c_3 = sample(x=c(rep(NA,N/2),combinations),size = N,replace = T),
  c_4 = sample(x=c(rep(NA,N/1),combinations),size = N,replace = T)) %>% 
  rowwise() %>% 
  mutate(c_all=list(c(c_1,c_2,c_3,c_4)[!is.na(c(c_1,c_2,c_3,c_4))]))

match_function <- function(x,y){
  result <- ifelse(length(intersect(x,y)),1,0)
  return(result)
}

outer(data$c_all, unlist(data$c_all)), match_function)

【问题讨论】:

  • 什么是pseudo_lastnames

标签: r matrix dplyr tidyverse


猜你喜欢
  • 2019-07-23
  • 1970-01-01
  • 2022-12-14
  • 2020-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多