Suzanne
Suzanne

Reputation: 83

Convert frequency vector to logical matrix

I would like to convert a frequency vector (i.e. the colSums() of a matrix) to one of the possible versions of the original logical matrix in R.

Something like:

    s <- c(1,2,3)
    # Some function of s
    # Example output:
         [,1] [,2] [,3]
    [1,]    0    0    1
    [2,]    1    0    0 
    [3,]    0    1    0
    [4,]    0    0    1
    [5,]    0    0    1
    [6,]    0    1    0

The order of rows is not important. Could someone give me a hint on how to do this?

Edit: Rowsums are always 1. The output can be considered a multinomial dataset where each row reflects an observation.

Upvotes: 0

Views: 57

Answers (3)

s_baldur
s_baldur

Reputation: 33548

# Input:
s <- c(1,2,3)
# ...
set.seed(1) # For reproducibility
nr <- sum(s)
nc <- length(s)
mat <- matrix(0L, nrow = nr, ncol = nc)
mat[cbind(seq_len(nr), sample(rep(seq_len(nc), s)))] <- 1L

# Output:
     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    0    1
[3,]    0    1    0
[4,]    0    0    1
[5,]    0    1    0
[6,]    0    0    1

Upvotes: 2

Gregor Thomas
Gregor Thomas

Reputation: 146050

s <- c(1,2,3)
result = matrix(0, nrow = max(s), ncol = length(s))
for (i in seq_along(s)) result[1:s[i], i] = 1
result
#      [,1] [,2] [,3]
# [1,]    1    1    1
# [2,]    0    1    1
# [3,]    0    0    1

Keeping rowsums as 1

s <- c(1,2,3)
result = matrix(0, nrow = sum(s), ncol = length(s))
result[cbind(1:sum(s), rep(seq_along(s), times = s))] = 1
result
#      [,1] [,2] [,3]
# [1,]    1    0    0
# [2,]    0    1    0
# [3,]    0    1    0
# [4,]    0    0    1
# [5,]    0    0    1
# [6,]    0    0    1

Upvotes: 2

kath
kath

Reputation: 7724

set.seed(523)

s <- c(1, 2, 3)
n <- 6

sapply(s, function(i) sample(c(rep(1, i), rep(0, n - i))))
#      [,1] [,2] [,3]
# [1,]    0    1    1
# [2,]    1    0    0
# [3,]    0    1    0
# [4,]    0    0    1
# [5,]    0    0    0
# [6,]    0    0    1

Upvotes: 2

Related Questions