I want to implement reshape(X3, [ ], 5) command which i use in matlab in R
I have X3.tif file ( 200 * 150*5)
nrows = 200 ncols= 150 and nbands = 5
I use this command to save tif in datafeame
a <- brick('X3.tif')
X3 is a 3D data but I want to save it as matrix of dimension
[ (200*150) * 5 ]
so that I have ( nbands as number of colums )
If a use :
A <- as.data.frame.matrix(a)
it stores matrix of dimension 200*150 and eliminates the nband =5
Thanks
I think that what you're looking for is this:
#Sample matrix
myMatrix <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8))
myMatrix
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
[5,] 5
[6,] 6
[7,] 7
[8,] 8
Try this:
myMatrix_new <- matrix(myMatrix, nrow = 2, byrow = TRUE)
myMatrix_new
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
Under help ?matrix you can find the arguments nrow, ncol, byrow which allows you to set the number of rows, columns and automatically, if you'd like.
Related
I am working in R, and I have a dataset that is a list of lists of matrices. Each sublist in the mainlist has two matrices of equal dimension (10 rows x 2 cols). I would like to rbind() each list of matrices into a single matrix (20 rows x 2 cols). But I do not want to combine every sublist into a single big matrix. Gonna try my best to write a sample code for it but the real data is pretty complex so I'll do my best.
> matrix_1 <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 5, ncol = 2, byrow = TRUE)
> matrix_2 <- matrix(c(9, 8, 7, 6, 5, 4, 3, 2, 1), nrow = 5, ncol = 2, byrow = TRUE)
> matrix_3 <- matrix(c(101, 91, 81, 71, 61, 51, 41, 31, 21, 11), nrow = 5, ncol = 2, byrow = TRUE)
> matrix_4 <- matrix(c(22, 20, 19, 18, 17, 16, 15, 14, 13, 12), nrow = 5, ncol = 2, byrow = TRUE)
> sublist_1 <- list(matrix_1, matrix_2)
[[1]]
[,1] [,2]
[1,] 1 5
[2,] 2 6
[3,] 1 3
[4,] 7 4
[5,] 8 3
[[2]]
[,1] [,2]
[1,] 10 9
[2,] 8 7
[3,] 6 5
[4,] 4 3
[5,] 2 1
> sublist_2 <- list(matrix_3, matrix_4)
[[1]]
[,1] [,2]
[1,] 101 91
[2,] 81 71
[3,] 61 51
[4,] 41 31
[5,] 21 11
[[2]]
[,1] [,2]
[1,] 22 20
[2,] 19 18
[3,] 17 16
[4,] 15 14
[5,] 13 12
> mainlist <- list(sublist_1, sublist_2)
What I really want is to make this:
> rbind(sublist_1[[1]], sublist_1[[2]])
[,1] [,2]
[1,] 1 5
[2,] 2 6
[3,] 1 3
[4,] 7 4
[5,] 8 3
[6,] 10 9
[7,] 8 7
[8,] 6 5
[9,] 4 3
[10,] 2 1
apply to all of the sublists in the mainlist.
I've tried to use various combinations of lapply, mapply, map, do.call, etc. to make it work, but either I don't know the right combinations or I need something else.
I've also noticed that rbind(sublist_1) does not work, which is making it difficult to use lapply. It has to be written as rbind(sublist_1[[1]], sublist_1[[2]]).
Thank you very much for your help.
Loop over the outer list, convert the inner list elements to data.frame and use do.call with rbind
out <- lapply(mainlist, function(x) do.call(rbind, lapply(x, as.data.frame)))
I am working with the hand-written zip codes dataset. I have loaded the dataset like this:
digits <- read.table("./zip.train",
quote = "",
comment.char = "",
stringsAsFactors = F)
Then I get only the ones:
ones <- digits[digits$V1 == 1, -1]
Right now, in ones I have 442 rows, with 256 column. I need to transform each row in ones to a 16x16 matrix. I think what I am looking for is a list of 16x16 matrix like the ones in this question:
How to create a list of matrix in R
But I tried with my data and did not work.
At first I tried ones <- apply(ones, 1, matrix, nrow = 16, ncol = 16) but is not working as I thought it was. I also tried lapply with no luck.
An alternative is to just change the dims of your matrix.
Consider the following matrix "M":
M <- matrix(1:12, ncol = 4)
M
# [,1] [,2] [,3] [,4]
# [1,] 1 4 7 10
# [2,] 2 5 8 11
# [3,] 3 6 9 12
We are looking to create a three dimensional array from this, so you can specify the dimensions as "row", "column", "third-dimension". However, since the matrix is constructed by column, you first need to transpose it before changing the dimensions.
`dim<-`(t(M), c(2, 2, nrow(M)))
# , , 1
#
# [,1] [,2]
# [1,] 1 7
# [2,] 4 10
#
# , , 2
#
# [,1] [,2]
# [1,] 2 8
# [2,] 5 11
#
# , , 3
#
# [,1] [,2]
# [1,] 3 9
# [2,] 6 12
though there are probably simple ways, you can try with lapply:
ones_matrix <- lapply(1:nrow(ones), function(i){matrix(ones[i, ], nrow=16)})
I have a matrix storing some indices and an other on with given values. I wanted to access these values by taping matrix[matrix] but I can't figure out what is produced.
A quick example :
indices = matrix(c(1,1,3,2,3,2), 3, 2)
data = matrix(1:9, 3, 3)
data[indices]
[1] 4 7 6
while I was expecting:
> rbind(data[1,indices[1,]], data[2, indices[2,]], data[3, indices[3,]])
[,1] [,2]
[1,] 1 4
[2,] 2 8
[3,] 9 6
I have no idea what R is doing.
Thanks !
I have
mat1 = matrix(c(2, 4, 3, 6, 7, 8), nrow=2, ncol=3)
mat2 = matrix(c(5, 6, 7, 1, 2, 3), nrow=2, ncol=3)
mat3 = matrix(c(8, 5, 8, 6, 7, 9), nrow=2, ncol=3)
which gives me 3 matrices:
[,1] [,2] [,3]
[1,] 2 3 7
[2,] 4 6 8
[,1] [,2] [,3]
[1,] 5 7 2
[2,] 6 1 3
[,1] [,2] [,3]
[1,] 8 8 7
[2,] 5 6 9
What I would like to do is compare the three matrices per row per first column, and select the row of the matrix that has the highest value on the first column.
For example: in row 1 column 1, matrix3 has the highest value (8) compared to matrix1 (2) and matrix2 (5). In row 2 column 1, matrix2 has the highest value (6). I would like to create a new matrix that copies the row of the matrix that has that highest value, resulting in:
[,1] [,2] [,3]
[1,] 8 8 7 <- From mat3
[2,] 6 1 3 <- From mat2
I know how to get a vector with the highest values from column 1, but I cannot get the whole row of the matrix copied into a new matrix. I have:
mat <- (mat1[1,])
which just copies the first row of the first matrix
[1] 2 3 7
I can select which number is the maximum number:
max(mat1[,1],mat2[,1],mat3[,1])
[1] 8
But I cannot seem to combine the two to return a matrix with the whole row.
Getting the code to loop for each row will be no problem, but I cannot seem to get it to work for the first row and as such, I am missing the essential code. Any help would be greatly appreciated. Thank you.
Are you working interactively? Do you manipulate multiple matrices spread in your workspace? A straightforward answer to your problem could be:
#which matrices have the largest element of column 1 in each row?
max.col(cbind(mat1[, 1], mat2[, 1], mat3[, 1]))
#[1] 3 2
rbind(mat3[1, ], mat2[2, ]) #use the above information to get your matrix
# [,1] [,2] [,3]
#[1,] 8 8 7
#[2,] 6 1 3
On a more ganeral use-case, a way could be:
mat_ls = list(mat1, mat2, mat3) #put your matrices in a "list"
which_col = 1 #compare column 1
which_mats = max.col(do.call(cbind, lapply(mat_ls, function(x) x[, which_col])))
do.call(rbind, lapply(seq_along(which_mats),
function(i) mat_ls[[which_mats[i]]][i, ]))
# [,1] [,2] [,3]
#[1,] 8 8 7
#[2,] 6 1 3
Probably not the prettiest solution
temp <- rbind(mat1, mat2, mat3)
rbind(temp[c(T,F),][which.max(temp[c(T,F),][, 1]),],
temp[c(F,T),][which.max(temp[c(F,T),][, 1]),])
## [,1] [,2] [,3]
## [1,] 8 8 7
## [2,] 6 1 3
You may also try:
a2 <- aperm(simplify2array( mget(ls(pattern="mat"))),c(3,2,1)) #gets all matrices with name `mat`
t(sapply(1:(dim(a2)[3]), function(i) {x1 <- a2[,,i]; x1[which.max(x1[,1]),]}))
# [,1] [,2] [,3]
#[1,] 8 8 7
#[2,] 6 1 3
I would like to apply on a matrix a function of both the value, the row index and the column index for every value in the matrix and get the transformed matrix.
For example
mat<-matrix(c(1,2,3,4),2,2)
mat
[,1] [,2]
[1,] 1 3
[2,] 2 4
f<-function(x,i,j){x+i+j}
mat2 <- my.apply(f,mat)
mat2
[,1] [,2]
[1,] 3 6
[2,] 5 8
The example above is for illustration purposes, f can be much more complex.
apply does not do the job, because of the way the extra arguments are handled.
apply(mat,1:2,f,seq_along(mat[,1]),seq_along(mat[1,]))
, , 1
[,1] [,2]
[1,] 3 4
[2,] 5 6
, , 2
[,1] [,2]
[1,] 5 6
[2,] 7 8
I can not find either a way with the lapply family. A for loop can do the job but it won't be efficient nor elegant.
Any suggestions?
Thanks
Try mapply
mat <- matrix(c(1, 2, 3, 4), 2, 2)
mat
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
matrix(mapply(function(x, i, j) x + i + j, mat, row(mat), col(mat)), nrow = nrow(mat))
## [,1] [,2]
## [1,] 3 6
## [2,] 5 8
Here is an ugly use of apply, just for some quick and dirty job. The trick is adding an additional column (or row) for row (or column) indices.
mat <- matrix(c(1, 2, 3, 4), 2, 2)
t(apply(cbind(mat, 1:nrow(mat)), 1, function(x){x[1:ncol(mat)] + 1:ncol(mat) + x[ncol(mat)+1]}))
## [,1] [,2]
##[1,] 3 5
##[2,] 6 8
If you have a function f(x, i, j) already, you can also try:
apply(cbind(mat, 1:nrow(mat)), 1, function(x){a = numeric(); for(j in 1:ncol(mat)){a[j] = f(x[j], x[ncol(mat)+1], j)}; a})