Suppose I have a 3 X 15 matrix "phi", and I want to create a vector of entries from phi corresponding to an i,j combination, where i is a length 900 vector of numbers 1:3, and j is a length 900 vector of numbers 1:15. In other words, I want a length 900 vector of phi values, where the first element is phi[i[1], j[1]], the second element would be phi[i[2], j[2]], etc.
My initial thought was phi_list <- phi[i, j], but that appears to give back every combination of i,j values. So, how would I go about constructing such a vector?
Thanks for any help!
In this case, we can use the index as a matrix with the i for row index and 'j' for column index
phi[cbind(i, j)]
#[1] 6 18 35
If we use the i and jvectors in the 'i', and 'j' it would return a matrix by including the rows and columns included in the index instead of picking the elements that matches the location
data
set.seed(24)
phi <- matrix(1:50, 5, 10)
i <- c(1, 3, 5)
j <- c(2, 4, 7)
Related
I have a vector [1:360] with integers and need to find the products of the first, second ... twelfth set of 30 elements. Ultimately, I need a function that gives me a vector [1:12] with the products of all twelve 30-element intervals.
I'm fairly new to R and have been stuck on this for too long.
A simple way to do this would be to turn your vector into a 30-row matrix and get the product of each column.
In the absence of a reproducible example, let's make one with a vector of 360 numbers drawn from a normal distribution:
set.seed(69)
vec <- rnorm(360)
We can turn vec into a 30 * 12 matrix by just doing matrix(vec, nrow = 30), which will fill the matrix by column. We then get the product of each column by using apply to apply the function prob to each column.
apply(matrix(vec, nrow = 30), 2, prod)
#> [1] -6.253460e-09 -4.413086e-09 -1.332389e-10 1.041448e-08 -1.779489e-08 1.255979e-10
#> [7] 3.463687e-13 -6.265196e-12 8.300651e-04 -1.041469e-10 4.256378e-09 1.439522e-09
I have a named vector of cumulated sums:
x <- sort(runif(20, 1, 10), decreasing = T)
names(x) <- LETTERS[1:20]
cumsums <- cumsum(x)
head(cumsums)
A B C D E F
9.902633 19.240766 28.531703 37.537920 46.065978 54.380480
How can i identify the positions at which the first value of cumsums exceeds a defined threshold (e.g. 25,50,75, 90)?
For a single threshold at a time, the following should work:
which(cumsums > 25)[1]
Unlike the which.max solution, it will return NA if there are no elements of cumsums greater than the threshold.
Of course, if your vector is very large or you need to look for multiple thresholds simultaneously, this may not be the most efficient solution.
I have an array
h <- array(0, c(100, 5, 9));
The length 9 vector is sort of an identification sequence. Given some i in 1:100, is there a way I can obtain the index j in 1:5 for which the length 9 sequence matches some vector V? ie. the index j such that
identical(h(i,j,1:9),V)
is true.
Edit: this is assuming that one of the 5 do match for the given i. It'd be even better if one can obtain the pair (i,j) for which it matches with V.
I would like to create a (N*M)-Incidence Matrix for a bipartite graph (N=M=200).
However, the following restrictions have to be considered:
Each column i ( 1 , ... , 200 ) has a column sum of g = 10
each row has a Row sum of h = 10
no multiedges (The values in the incidence Matrix only take on the values [0:1]
So far I have
M <- 200; # number of rows
N <- 200; # number of colums
g <- 10
I <- matrix(sample(0:1, M*N, repl=T, prob= c(1-g/N,g/N)), M, N);
Does anybody has a solution?
Here's one way to do what you want. First the algorithm idea, then its implementation in R.
Two step Algorithm Idea
You want a matrix of 0's and 1's, with each row adding up to be 10, and each column adding up to be 10.
Step 1: First,create a trivial solution as follows:
The first 10 rows have 1's for the first 10 elements, then 190 zeros.
The second set of ten rows have 1's from the 11th to the 20th element and so on.
In other words, a feasible solution is to have a 200x200 matrix of all 0's, with dense matrices of 10x10 1's embedded diagonally, 20 times.
Step 2: Shuffle entire rows and entire columns.
In this shuffle, the rowSum and columnSums are maintained.
Implementation in R
I use a smaller matrix of 16x16 to demonstrate. In this case, let's say we want each row and each column to add up to 4. (This colsum has to be integer divisible of the larger square matrix dimension.)
n <- 4 #size of the smaller square
i <- c(1,1,1,1) #dense matrix of 1's
z <- c(0,0,0,0) #dense matrix of 0's
#create a feasible solution to start with:
m <- matrix(c(rep(c(i,z,z,z),n),
rep(c(z,i,z,z),n),
rep(c(z,z,i,z),n),
rep(c(z,z,z,i),n)), 16,16)
#shuffle (Run the two lines following as many times as you like)
m <- m[sample(16), ] #shuffle rows
m <- m[ ,sample(16)] #shuffle columns
#verify that the sum conditions are not violated
colSums(m); rowSums(m)
#solution
print(m)
Hope that helps you move forward with your bipartite igraph.
I have an array of z-scores that is structured like num [1:27, 1:11, 1:467], so there are 467 entries with 27 rows and 11 columns. Is there a way that I can make a list from this array? For example a list of entries which contain a z-score over 2.0 (not just a list of z scores, a list which identifies which 1:467 entries have z > 2).
Say that your array is called z in your R session. The function you are looking for is which with the argument arr.ind set to TRUE.
m <- which(z > 2, arr.ind=TRUE)
This will give you a selection matrix, i.e. a matrix with three columns, each line corresponding to an entry with a Z-score greater than 2. To know the number of Z-scores greater than 2 you can do
nrow(m)
# Note that 'sum(z > 2)' is easier.
and to get the values
z[m]
# Note that 'z[z > 2]' is easier