我有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
.
.
.
答案 0 :(得分:1)
使用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)
答案 1 :(得分:1)
这是使用table
lst <- mget(ls(pattern = "^x\\d+$"))
mat <- t(sapply(lst, function(x) table(factor(x, levels = 1:max(sapply(lst, max))))))
# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#x1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#x2 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
#x3 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#x4 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
#x5 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#x1 0 0 0 0 0 1 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 0 0 0 0 0 0
#x3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
#x4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
#x5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
# 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#x1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 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 0 0 0 0 0 1
#x3 0 0 0 0 0 0 0 0 0 0 0 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#x5 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# 79 80 81 82 83 84 85 86 87 88 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)
答案 2 :(得分:1)
使用行/列组合在一次操作中分配所有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 ...
答案 3 :(得分:1)
使用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)