创建具有指标功能的表以进行组合
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了创建具有指标功能的表以进行组合相关的知识,希望对你有一定的参考价值。
我有50个组合,我想制作一个有0或1的表,其值。例如,
x1= c(01, 34, 67, 09)
x2= c(01, 22, 09, 78)
x3= c(09, 83, 45, 82)
x4= c(23, 89, 04, 44)
x5= c(04, 44, 97, 56)
.
.
.
x50=c(78, 90, 88, 00)
通缉:
01 02 03 04 ... 09 ... 34 ... 67 ... 99
1 0 0 0 1 1 1 0 # for the first row
.
.
.
答案
矩阵索引的好机会,使用行/列组合在单个操作中分配所有1:
xl <- mget(paste0("x", 1:5))
mat <- matrix(0, nrow=length(xl), ncol=99)
mat[cbind(rep(seq_along(xl), lengths(xl)), unlist(xl))] <- 1
mat
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] ...
#[1,] 1 0 0 0 0 0 0 0 1 0
#[2,] 1 0 0 0 0 0 0 0 1 0
#[3,] 0 0 0 0 0 0 0 0 1 0
#[4,] 0 0 0 1 0 0 0 0 0 0
#[5,] 0 0 0 1 0 0 0 0 0 0 ...
另一答案
使用tidyverse
的解决方案。我只使用前五种组合作为例子。 dat
是最终输出。
x1 <- c(01, 34, 67, 09)
x2 <- c(01, 22, 09, 78)
x3 <- c(09, 83, 45, 82)
x4 <- c(23, 89, 04, 44)
x5 <- c(04, 44, 97, 56)
library(tidyverse)
# Get the five combinations as a list
dat_list <- mget(x = paste0("x", 1:5))
dat <- dat_list %>%
# Convert to a tibble
as_tibble() %>%
# Convert to long format
gather(x, value) %>%
# Create an indicator with presence = 1L
mutate(indicator = 1L) %>%
# Complete the combination between x and value, fill the NA with 0
complete(x, value = 1:99, fill = list(indicator = 0L)) %>%
# Convert to wide format
spread(value, indicator) %>%
# Remove x
select(-x)
另一答案
这是使用table
的基本R解决方案
lst <- mget(ls(pattern = "^x\d+$"))
mat <- t(sapply(lst, function(x) table(factor(x, levels = 1:max(sapply(lst, max))))))
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89 90 91 92 93 94 95 96 97
#x1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#x2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#x3 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#x4 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
#x5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
样本数据
x1= c(01, 34, 67, 09)
x2= c(01, 22, 09, 78)
x3= c(09, 83, 45, 82)
x4= c(23, 89, 04, 44)
x5= c(04, 44, 97, 56)
另一答案
使用sapply
的另一个基本R选项假设您知道这些向量可以采用的最大值(在此处考虑99)
t(sapply(mget(paste0("x", 1:5)), function(x) +(1:99 %in% x)))
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] ......
#x1 1 0 0 0 0 0 0 0 1 ......
#x2 1 0 0 0 0 0 0 0 1 ......
#x3 0 0 0 0 0 0 0 0 1 ......
#x4 0 0 0 1 0 0 0 0 0 ......
#x5 0 0 0 1 0 0 0 0 0 ......
数据
x1 <- c(01, 34, 67, 09)
x2 <- c(01, 22, 09, 78)
x3 <- c(09, 83, 45, 82)
x4 <- c(23, 89, 04, 44)
x5 <- c(04, 44, 97, 56)
以上是关于创建具有指标功能的表以进行组合的主要内容,如果未能解决你的问题,请参考以下文章