I want to produce a matrix which holds all possible combinations of a vector x of integers from 1 to the respective number.
The length of the vector x may change.
For this sample vector:
x = c(3,8,2)
I want the result to look something like this:
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 1 1 2
[3,] 1 2 1
...
[48,] 3 8 2
I understand expand.grid does the job, however, I can't seem to find the parameters which allow for different sets in each column.
We get the sequence of each element (seq) and do expand.grid
out <- expand.grid(lapply(x, seq))
dim(out)
#[1] 48 3
Related
I have a large array with dimensions data[1:10,1:50,1:1000]. I would like to swap out the 5th row of all the matrices with new data with the dimensions new_data[1,1:50,1:1000].
So far I have tried to pull the array apart and put it back together:
data1<-data[1:4,1:50,1:1000]
data2<-data[6:10,1:50,1:1000]
combined_data<-rbind(data1,new_data,data2)
However rbind doesn't seem to be appropriate here and returns a large matrix rather than a large array with dimensions[1:10,1:50,1:1000]
On request here is a simple example:
vec1<-1:4
vec2<-c(1,2,2,4,1,2,2,4)
data_array<-array(c(vec1,vec2),dim=c(4,3,10))
data_array[,,1] # visualizing one of the 10 matrix - say they error is in row 3 where we would expect all 3s
new_data<-array(c(3,3,3),dim=c(1,3,10))
new_data[,,1] # correct data that we want to swap into row 3 of all the matrices
array2<-data_array[1:2,,] #correct data from original array
array3<-array(data_array[4,,],dim=c(1,3,10)) #correct data from original array
combined_data <- rbind(array2,new_data,array3) # attempting to combine and new_data into the correct row
However this results in a data with the dimensions [1:3,1:60], where I am aiming for the exact same dimensions as the original data_array ([1:4,1:3,1:10]) but with the new_data swapped in at row 3 of each matrix
Try with abind from "abind" package.
library(abind)
array4 <- abind(array2,new_data,along=1)
final_data <- abind(array4,array3,along=1)
The reference is as follows:
http://math.furman.edu/~dcs/courses/math47/R/library/abind/html/abind.html
Since an array is really just a vector with dimensions, you can replace every 4th value (the number of rows in each stratum), starting at the 3rd value (the row you want to replace), with the new_data
data_array[seq(3, by=dim(data_array)[1], to=length(data_array))] <- new_data
data_array
#, , 1
#
# [,1] [,2] [,3]
#[1,] 1 1 1
#[2,] 2 2 2
#[3,] 3 3 3
#[4,] 4 4 4
#
#, , 2
#
# [,1] [,2] [,3]
#[1,] 1 1 1
#[2,] 2 2 2
#[3,] 3 3 3
#[4,] 4 4 4
#...
Given the sequence 1 2 3 4, I would like to generate a matrix of pairs
1 2
2 3
3 4
to use as indexes for another matrix. What would be the fastest way to achieve this?
You could use embed(), reversing the columns on the output.
embed(1:4, 2)[, 2:1]
# [,1] [,2]
# [1,] 1 2
# [2,] 2 3
# [3,] 3 4
My aim is to delete specific positions in a matrix according to a vector. Just giving you a small example.
Users_pos <- c(1,2)
Items_pos <- c(3,2)
Given a Matrix A:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
My aim according to the two Vectors User_pos and Item_pos is to delete the following values
A[1,3] and A[3,2]
I'm wondering if there's a possibility to do so without typing in the values for rows and columns by hand.
You can index k elements in a matrix A using A[X], where X is a k-row, 2-column matrix where each row is the (row, col) value of the indicated element. Therefore, you can index your two elements in A with the following indexing matrix:
rbind(Users_pos, Items_pos)
# [,1] [,2]
# Users_pos 1 2
# Items_pos 3 2
Using this indexing, you could choose to extract the information current stored with A[X] or replace those elements with A[X] <- new.values. If you, for instance, wanted to replace these elements with NA, you could do:
A[rbind(Users_pos, Items_pos)] <- NA
A
# [,1] [,2] [,3]
# [1,] 1 NA 3
# [2,] 4 5 6
# [3,] 7 NA 9
I have a matrix where the colnames are sample names and I have created a vector of the colnames.
I also have a vector of sample names I need to subset from the matrix which I have found are not in the same order as the colnames of the matrix.
To subset the matrix I need to find which columns in the matrix correspond to the samples I need.
To illustrate this:
colnames <- c("A","B","C","D","E","F","G","H","I")
sample_names<- c("B","D","I")
I need a way to get R to return the position information such that for the example sample names "B","D","I", the colnames position is: [1] 2 4 9
Sample data:
> m=matrix(rep(1:4,3),ncol=4)
> colnames(m)<-c("A","C","D","B")
> m
A C D B
[1,] 1 4 3 2
[2,] 2 1 4 3
[3,] 3 2 1 4
> vec<-c("A","B")
> vec
[1] "A" "B"
To answer your exact question, use which, it will return the index of TRUE values in a logical vector.
> which(colnames(m)==vec)
[1] 1 4
But as your goal seems to be subsetting the matrix, just use directly the sample names vector to get it like this:
> m[, vec]
A B
[1,] 1 2
[2,] 2 3
[3,] 3 4
As fast as possible, I would like to replace the first zeros in some rows of a matrix with values stored in another vector.
There is a numeric matrix where each row is a vector with some zeros.
I also have two vectors, one containing the rows, in what to be replaced, and another the new values: replace.in.these.rows and new.values. Also, I can generate the vector of first zeroes with sapply
mat <- matrix(1,5,5)
mat[c(1,8,10,14,16,22,14)] <- 0
replace.in.these.rows <- c(1,2,3)
new.values <- c(91,92,93)
corresponding.poz.of.1st.zero <- sapply(replace.in.these.rows,
function(x) which(mat [x,] == 0)[1] )
Now I would like something that iterates over the index vectors, but without a for loop possibly:
matrix[replace.in.these.rows, corresponding.poz.of.the.1st.zero ] <- new.values
Is there a trick with indexing more than simple vectors? It could not use list or array(e.g.-by-column) as index.
By default R matrices are a set of column vectors. Do I gain anything if I store the data in a transposed form? It would mean to work on columns instead of rows.
Context:
This matrix stores contact ID-s of a network. This is not an adjacency matrix n x n, rather n x max.number.of.partners (or n*=30) matrix.
The network uses edgelist by default, but I wanted to store the "all links from X" together.
I assumed, but not sure if this is more efficient than always extract the information from the edgelist (multiple times each round in a simulation)
I also assumed that this linearly growing matrix form is faster than storing the same information in a same formatted list.
Some comments on these contextual assumptions are also welcome.
Edit: If only the first zeros are to be replace then this approach works:
first0s <-apply(mat[replace.in.these.rows, ] , 1, function(x) which(x==0)[1])
mat[cbind(replace.in.these.rows, first0s)] <- new.values
> mat
[,1] [,2] [,3] [,4] [,5]
[1,] 91 1 1 0 1
[2,] 1 1 1 1 92
[3,] 1 93 1 1 1
[4,] 1 1 0 1 1
[5,] 1 0 1 1 1
Edit: I thought that the goal was to replace all zeros in the chosen rows and this was the approach. A completely vectorized approach:
idxs <- which(mat==0, arr.ind=TRUE)
# This returns that rows and columns that identify the zero elements
# idxs[,"row"] %in% replace.in.these.rows
# [1] TRUE TRUE FALSE FALSE TRUE TRUE
# That isolates the ones you want.
# idxs[ idxs[,"row"] %in% replace.in.these.rows , ]
# that shows what you will supply as the two column argument to "["
# row col
#[1,] 1 1
#[2,] 3 2
#[3,] 1 4
#[4,] 2 5
chosen.ones <- idxs[ idxs[,"row"] %in% replace.in.these.rows , ]
mat[chosen.ones] <- new.values[chosen.ones[,"row"]]
# Replace the zeros with the values chosen (and duplicated if necessary) by "row".
mat
#---------
[,1] [,2] [,3] [,4] [,5]
[1,] 91 1 1 91 1
[2,] 1 1 1 1 92
[3,] 1 93 1 1 1
[4,] 1 1 0 1 1
[5,] 1 0 1 1 1