matrix_1 = matrix(rep(c("p","r"),6), c(rep("control",6), rep("concussion",6)),
nrow = 12, ncol = 2)
It says invalid byrow argument (I want it by column and byrow is F by default) so I basically want the first column to have p and r repeated 6 times for a total of 12 rows and the second column to have Control in first 6 rows and Concussion in the next 6
The usage of matrix is
matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE,
dimnames = NULL)
So, if the data is being split up like in the OP's post, one of the arguments that is not specified would be incorrectly being labelled as byrowi.e. in the OP's code,
data = rep(c("p","r"),6)
The nrow and ncol arguments are specified, leaving the other two arguments in the order to take up the rest of the input i.e.
c(rep("control",6), rep("concussion",6))
would be mistakenly taken as argument for byrow. However, byrow takes a logical argument and it is a potential reason for the error.
matrix_1 = matrix(rep(c("p","r"),6), c(rep("control",6), rep("concussion",6)),
nrow = 12, ncol = 2)
Error in matrix(rep(c("p", "r"), 6), c(rep("control", 6),
rep("concussion", : invalid 'byrow' argument
If we specify the byrow = FALSE, then the error will be based on the dimnames
matrix_1 = matrix(rep(c("p","r"),6), c(rep("control",6), rep("concussion",6)),
nrow = 12, ncol = 2, byrow = FALSE)
Error in matrix(rep(c("p", "r"), 6), c(rep("control", 6),
rep("concussion", : 'dimnames' must be a list
As there is only a single data argument, we need to concatenate the strings
matrix_1 <- matrix(data = c(rep(c("p","r"),6),c(rep("control",6),rep("concussion",6))),
nrow=12,ncol=2)
Now, as byrow = FALSE by default it will not get affected
Related
How do I compute the dot product for
movies <- matrix(c(3,1,1,2,1,4,3,1,1,3), ncol = 2, byrow = T)
users <- matrix(c(1,0,0,1,1,0,1,1), ncol = 2, byrow = T)
expected <- matrix(c(3,1,1,3,1,
1,2,4,1,3,
3,1,1,3,1,
4,3,5,4,4), ncol = 5, byrow = T)
This example is from
https://www.youtube.com/watch?v=ZspR5PZemcs
at 12:55
Answer is
users %*% t(movies)
I have been confused by "dot product" I think this is something different from matrix multiplication.
I want create such matrix
dat <- matrix(
"an_no" = c(14, 17),
"an_yes" = c(3, 1),
row.names = c("TL-MCT-t", "ops"),
stringsAsFactors = FALSE
)
but i get error unused arguments.
What i did wrong and how perform correct matrix with such arguments?
as.matrix didn't help.
Thanks for your help.
You are using the arguments that you would use to build a data frame. If you want a matrix using this syntax you can do:
dat <- as.matrix(data.frame(
an_no = c(14, 17),
an_yes = c(3, 1),
row.names = c("TL-MCT-t", "ops")))
dat
#> an_no an_yes
#> TL-MCT-t 14 3
#> ops 17 1
You don't need the stringsAsFactors = FALSE because none of your data elements are strings, and in any case, stringsAsFactors is FALSE by default unless you are using an old version of R. You also don't need quotation marks around an_no and an_yes because these are both legal variable names in R.
The matrix function estructure is this:
matrix(data = NA,
nrow = 1,
ncol = 1,
byrow = FALSE,
dimnames = NULL)
Appears you're trying to create a data.frame
data.frame(row_names = c("TL-MCT-t", "ops"),
an_no = c(14,17),
an_yes = c(3,1)
)
I have an array Q which has size nquantiles by nfeatures by nfeatures. In this, essentially the slice Q[1,,] would give me the first quantile of my data, across all nfeatures by nfeatures of my data.
What I am interested in, is using another matrix M (again of size nfeatures by nfeatures) which represents some other data, and asking the question to which quantile do each of the elements in M lie in Q.
What would be the quickest way to do this?
I reckon I could do double for loop across all rows and columns of the matrix M and come up with a solution similar to this: Finding the closest index to a value in R
But doing this over all nfeatures x nfeatures values will be very inefficient. I am hoping that there might exist a vectorized way of approaching this problem, but I am at a lost as to how to approach this.
Here is a reproducible way of the slow way I can approach the problem with O(N^2) complexity.
#Generate some data
set.seed(235)
data = rnorm(n = 100, mean = 0, sd = 1)
list_of_matrices = list(matrix(data = data[1:25], ncol = 5, nrow = 5),
matrix(data = data[26:50], ncol = 5, nrow = 5),
matrix(data = data[51:75], ncol = 5, nrow = 5),
matrix(data = data[76:100], ncol = 5, nrow = 5))
#Get the quantiles (5 quantiles here)
Q <- apply(simplify2array(list_of_matrices), 1:2, quantile, prob = c(seq(0,1,length = 5)))
#dim(Q)
#Q should have dims nquantiles by nfeatures by nfeatures
#Generate some other matrix M (true-data)
M = matrix(data = rnorm(n = 25, mean = 0, sd = 1), nrow = 5, ncol = 5)
#Loop through rows and columns in M to find which index of the array matches up closest with element M[i,j]
results = matrix(data = NA, nrow = 5, ncol = 5)
for (i in 1:nrow(M)) {
for (j in 1:ncol(M)) {
true_value = M[i,j]
#Subset Q to the ith and jth element (vector of nqauntiles)
quantiles = Q[,i,j]
results[i,j] = (which.min(abs(quantiles-true_value)))
}
}
'''
I have an empty matrix of the following form:
Empty_Matrix = matrix( NA ,nrow = 3, ncol = 2, byrow = TRUE, dimnames = list(c("a","b","c"),c("aa","bb")) )
aa bb
a NA NA
b NA NA
c NA NA
I would like to fill each element of this matrix which another matrix e.g:
Empty_Matrix[,] = list(matrix(0,nrow = 4, ncol=1))
This actually works although, I loose the structure of the row and column names as it shown in the following console screen-shot:
Contrary, if I use the following lines of code
Empty_Matrix = matrix( list(matrix(0,nrow = 4, ncol=2)) ,nrow = 3, ncol = 2, byrow = TRUE, dimnames = list(c("a","b","c"),c("aa","bb")))
the desired output is retrieved:
My question is if it is possible to use a similar line of code such as
Empty_Matrix[,] = list(matrix(0,nrow = 4, ncol=1))
(where Empty_Matrix has already been created with NA elements) and have the console output of the second image.
In R, I would like to get a list of all vectors of length from 1 to 3 made out of numbers 1, 2, 3, 4, 5. In total 160 vectors. I have been trying with this:
library(iterpc)
I3 = iterpc(5, 3, ordered = TRUE, replace = TRUE)
getall(I3)
I2 = iterpc(5, 2, ordered = TRUE, replace = TRUE)
getall(I2)
I1 = iterpc(5, 1, ordered = TRUE, replace = TRUE)
getall(I1)
But I don't know how to make a list of vectors from the results I get.