1

Here is my code:

n <- 10
set.seed(100)
d <- rep(NA, n)
d[1] <- 0
y <- runif(n)
a <- 5

for (i in (2:(length(y)+1))) {
 d[i] <- d[i-1] + y[i-1]
}

store.x <- NULL
for(j in 1:a) {
 x <- runif(1, min = 0, max = sum(y))
  for (i in 1:(length(y))) {
   if(x <= d[i+1] && x > d[i]) {
    store.x[j] <- i
    break
  }
 }
}
store.x

Now store.x prints out 7, 9, 4, 6, 8. I want to be able to put these into a matrix where the numbers that store.x prints correspond to the columns and the row is in order of the numbers. So the first entry would be in row 1 column 7, next would be row 2 column 9 and so on. I want to start with a n by n matrix filled with zeros and then add one the row/column that these numbers are in. I'm not sure how to go about doing this. Any help would be appreciated!

1
  • m <- matrix(0, n, n); m[cbind(seq_along(store.x), store.x)] <- store.x ? what exactly are you storing in the new matrix?
    – rawr
    Mar 30, 2016 at 15:22

1 Answer 1

2

So creating a matrix mt that will be filled and then NA's changed to zeros.

n <- 10
set.seed(100)
d <- rep(NA, n)
d[1] <- 0
y <- runif(n)
a <- 220
mt = matrix(nrow = n, ncol = n)
mt[is.na(mt)] = 0
for (i in (2:(length(y)+1))) {
    d[i] <- d[i-1] + y[i-1]
}

store.x <- NULL
for(j in 1:a) {
    x <- runif(1, min = 0, max = sum(y))
    for (i in 1:(length(y))) {
        if(x <= d[i+1] && x > d[i]) {
            store.x[j] <- i

            if(length(which(i == store.x)) > 1){
                mt[which(mt[,i] != 0),i] = mt[which(mt[,i] != 0),i] + 1
            } else {
                mt[(which(rowSums(mt) == 0)[1]),i] = 1
            }

            break
        }
    }
}

what i added was this following logic

if(length(which(i == store.x)) > 1){
                    mt[which(mt[,i] != 0),i] = mt[which(mt[,i] != 0),i] + 1
                } else {
                    mt[(which(rowSums(mt) == 0)[1]),i] = 1
                }

if the number created exists in store.x more than once then we find the existing entry (column corresponds to i and row will be the one which is not 0). If the number does not exist we then find the first row which has no entry and use that.

4
  • "creating a data frame" Where does the code create a data.frame?
    – Roland
    Mar 30, 2016 at 15:06
  • @zacdav This is a good start. But instead of mt[j,i] = i I want it to equal 1 but also have the property that you can add 1 when ever there is a duplicate in the same row/column. So if there are two values of 7 in my vector "store.x" in my previous example, then row 1 column 7 would have a value of 1 + 1.
    – Killian
    Mar 30, 2016 at 15:17
  • @killian so you mean that the 7 would become a 14 if a 7 already has occurred? Or do you only need to place a 1 in the matrix, not the actual value.
    – zacdav
    Mar 30, 2016 at 15:20
  • @zacdav I only need a one in the matrix. So mt[j,i] = 1 is sort of what I want. In your example the value in row 1, column 7 would become a 2 if the 7 has already occurred.
    – Killian
    Mar 30, 2016 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.