I have one matrix a with 24 rows and 44 columns and another one b with 44 rows and one column. I would like to multiply the first row of matrix b with the entire column of matrix a, and the second row of matrix b with the entire column of matrix a and so forth. How can I do that?
We can replicate the elements in the second matrix ('m2') to make the lengths same as in 'm1' and then do the multiplication.
m1*m2[col(m1)]
For replicating the elements, we used col, which returns the numeric index of the columns of the matrix ('m1')
col(m1)
# [,1] [,2] [,3] [,4]
#[1,] 1 2 3 4
#[2,] 1 2 3 4
#[3,] 1 2 3 4
#[4,] 1 2 3 4
#[5,] 1 2 3 4
By doing m2[col(m1)], the first element in 'm2' i.e. row1 column1 element is replicated 5 times, second 5 times, and so on.
m2[col(m1)]
#[1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4
data
m1 <- matrix(1:20, ncol=4)
m2 <- matrix(1:4, nrow=4)
This alternative uses vector recycling:
t(t(m1) * as.vector(m2))
Since the vector is recycled in the same way a matrix is filled (by columns), we need to first transpose m1 and then transpose the result again.
Related
I want to extract specific elements column wise from the matrix A with the information from a character vector B (contain elements in the row names of the matrix) such as:
A <- matrix(seq(1,12),ncol=4)
rownames(A) <- letters[1:3]
A
[,1] [,2] [,3] [,4]
a 1 4 7 10
b 2 5 8 11
c 3 6 9 12
B <- c("a","c","c","b")
I want to get 1,6,9,11. Thanks :)
Two possible ways:
> A[cbind(match(B, rownames(A)), seq_len(ncol(A)))]
[1] 1 6 9 11
>
> diag(A[B, seq_along(B)]) # or diag(A[B, seq_len(ncol(A))])
[1] 1 6 9 11
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 data in a csv file in a single column with 6954 values. I want to split this column into multiple columns such that each column has 122 data and next column has the next 122 data and so on. I guess, I will have a final matrix of 122 rows and 57 columns. Any help will be appreciated.
Thanks
Like this ?
x <- rep(1:122, 5)
xx <- matrix(x, nrow=122)
xx[1:5, ]
[,1] [,2] [,3] [,4] [,5]
[1,] 1 1 1 1 1
[2,] 2 2 2 2 2
[3,] 3 3 3 3 3
[4,] 4 4 4 4 4
[5,] 5 5 5 5 5
Or this will do the trick as well:
x = 1:6954
dim(x) <- c(122, 57)
A column can be split using the colsplit function which is part of the reshape package
http://r.ramganalytics.com/r/split-a-column-by-a-character-using-colsplit-function/
I have a matrix
[,1] [,2]
[1,] 2 3
[2,] 3 5
[3,] 7 9
[4,] 11 3
[5,] 11 8
and I want to merge row 1 2 4 5 by their common value.
the result should be output
2 3 5 11 8
Test case:
m <- matrix(c(2,3,7,11,11,3,5,9,3,8),ncol=2)
I'm not sure this is what you want, but it gives the right answer:
unique(c(t(m[c(1,2,4,5),])))
Only two tricky bits here:
need to use c() to collapse the matrix into a single vector
need to use t() to get the matrix collapsed row-wise rather than column-wise to get the ordering as you specified.