Mark Miller
Mark Miller

Reputation: 13113

assign vector elements to matrix

I wish to assign vector elements to a matrix. In my example I have a population of 10 kinds of fruit available for purchase, in my.fruit. A sample of five pieces of fruit are purchased in the sequence shown in sequence.of.purchased.item. I want to create a matrix containing the sequence in which the fruit was purchased as shown in desired.result.

Here is a somewhat long description of how desired.result is constructed. The vector my.fruit is essentially the row names of the matrix desired.result. The first plum purchased was the first piece of fruit purchased. The sequence number of 1 is placed in the first column of the fifth row, the row representing plums. The second plum purchased was the third piece of fruit purchased. The sequence number of 3 is placed in the second column of the plum row. The first apple purchased was the second piece of fruit purchased. So, the number 2 is placed in the first column of the first row, the row representing apples. The fourth piece of purchased fruit was an orange. So, a 4 is placed in the first column of the second row, the row representing oranges. The tenth row represents olives, but no olives were purchased. Similarly, no cherries, peaches, apricots, pears, grapefruit or figs were purchased. So, their rows are all zero in desired.result.

my.fruit <- c('apple', 'orange', 'cherry', 'peach', 'plum',
              'apricot', 'pear', 'grapefruit', 'fig', 'olive')

sequence.of.purchased.item <- c('plum', 'apple', 'plum', 'orange', 'plum')

desired.result <- matrix(c(
    2,    0,    0,
    4,    0,    0,
    0,    0,    0,
    0,    0,    0,
    1,    3,    5,
    0,    0,    0,
    0,    0,    0,
    0,    0,    0,
    0,    0,    0,
    0,    0,    0), ncol = 3, byrow = TRUE)

I can obtain the row and column indices for each piece of purchased fruit using:

purchase.order.row <- match(sequence.of.purchased.item, my.fruit)
purchase.order.row
#[1] 5 1 5 2 5

purchase.order.col <- sapply(1:length(sequence.of.purchased.item), 
     function(i) {sum(sequence.of.purchased.item[i] == sequence.of.purchased.item[1:i])})
purchase.order.col
#[1] 1 1 2 1 3

Here I attempt to assign the sequence number of each purchased fruit to an output matrix using sapply:

my.output <- matrix(0, ncol = 3, nrow = 10)

sapply(1:5, function(x) my.output[purchase.order.row[x], 
                                  purchase.order.col[x]] = x)

However, the sapply statement is not returning the desired output.

my.output
      [,1] [,2] [,3]
 [1,]    0    0    0
 [2,]    0    0    0
 [3,]    0    0    0
 [4,]    0    0    0
 [5,]    0    0    0
 [6,]    0    0    0
 [7,]    0    0    0
 [8,]    0    0    0
 [9,]    0    0    0
[10,]    0    0    0

Upvotes: 0

Views: 223

Answers (2)

nsinghphd
nsinghphd

Reputation: 2022

Use <<- instead of = inside your sapply function so that sapply knows to assign x to a globally defined variable instead of function internal.

Upvotes: -1

joran
joran

Reputation: 173577

This will probably be much easier:

my.output <- matrix(0, ncol = 3, nrow = 10)
i <- cbind(purchase.order.row,purchase.order.col)
my.output[i] <- 1:5

Upvotes: 2

Related Questions