Hi,
I have a 100x15 matrix and in each row a set of 15 random numbers out of 25.
for example:
b <- c(1:25)
a <-matrix(0,10,15)
for (i in 1:10){
a[i,] <- sample(b,15,replace = FALSE)
}
I would like to create another matrix (25x100), for example "d" with the
probability of each number from the first matrix
Therefore I need to track, for example, if number 1 is present in the first
row (d[1,1]) (which would give me an probability of 1 out of 1).
Then, track again if number 1 is present on the second row (d[2,1]) (And if
not, the probability would be 1 out of 2 = 0.5)...and so on for all the 25
collumns (25 numbers) and all the 100 rows.
Could anybody help how to do it??
Thanks in advance
Marcio
Therefore I need to track, for example, if number 1 is present in the first
row (d[1,1]) (which would give >me an probability of 1 out of 1).
Just to make it clear, I need to track on matrix "a" if number 1 is present
in the first row to fill the spot d[1,1] where the first collumn is related
to number 1 and the first row, related to row 1 on matrix a
Thanks again
M?rcio Resende wrote:
Hi,
I have a 100x15 matrix and in each row a set of 15 random numbers out of
25.
for example:
b <- c(1:25)
a <-matrix(0,10,15)
for (i in 1:10){
a[i,] <- sample(b,15,replace = FALSE)
}
I would like to create another matrix (25x100), for example "d" with the
probability of each number from the first matrix
Therefore I need to track, for example, if number 1 is present in the
first row (d[1,1]) (which would give me an probability of 1 out of 1).
Then, track again if number 1 is present on the second row (d[2,1]) (And
if not, the probability would be 1 out of 2 = 0.5)...and so on for all the
25 collumns (25 numbers) and all the 100 rows.
Could anybody help how to do it??
Thanks in advance
Marcio
On Jan 11, 2010, at 9:38 PM, M?rcio Resende wrote:
Hi,
I have a 100x15 matrix and in each row a set of 15 random numbers
out of 25.
for example:
b <- c(1:25)
a <-matrix(0,10,15)
for (i in 1:10){
a[i,] <- sample(b,15,replace = FALSE)
}
I would like to create another matrix (25x100), for example "d" with
the
probability of each number from the first matrix
Therefore I need to track, for example, if number 1 is present in
the first
row (d[1,1]) (which would give me an probability of 1 out of 1).
Then, track again if number 1 is present on the second row (d[2,1])
(And if
not, the probability would be 1 out of 2 = 0.5)...and so on for all
the 25
collumns (25 numbers) and all the 100 rows.
Could anybody help how to do it??
On Jan 11, 2010, at 10:16 PM, David Winsemius wrote:
On Jan 11, 2010, at 9:38 PM, M?rcio Resende wrote:
Hi,
I have a 100x15 matrix and in each row a set of 15 random numbers
out of 25.
for example:
b <- c(1:25)
a <-matrix(0,10,15)
for (i in 1:10){
a[i,] <- sample(b,15,replace = FALSE)
}
I would like to create another matrix (25x100), for example "d"
with the
probability of each number from the first matrix
Therefore I need to track, for example, if number 1 is present in
the first
row (d[1,1]) (which would give me an probability of 1 out of 1).
Then, track again if number 1 is present on the second row (d[2,1])
(And if
not, the probability would be 1 out of 2 = 0.5)...and so on for all
the 25
collumns (25 numbers) and all the 100 rows.
Could anybody help how to do it??
Maybe:
apply(a, 2, table)[[1]]
1 5 6 7 10 16 21 22 25
1 2 1 1 1 1 1 1 1
apply(a, 2, table)[[1]]/10
snipped out earlier intermediates
The reason the d matrix looks wrong is that I failed to "zero" it
after an incorrect application of the loop logic. Ignore this column
1. I think the "answer" will be what you asked for.
for(i in 1:15) {d[as.numeric(names(lapply(apply(a, 2, table), "/",