I have a matrix (V), which looks like this
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[V1,] 37 15 30 3 4 11 35 31
[V2,] 44 31 45 30 24 39 1 18
[V3,] 39 49 7 36 14 43 26 24
[V4,] 45 31 26 33 12 47 37 15
[V5,] 23 27 34 29 30 34 17 4
[V6,] 9 46 39 34 8 43 42 37
I have another matrix (X)
[,1] [,2] [,3] [,4] [,5] [,6]
[X1,] 37 15 21 3 4 11 35 31
[X2,] 37 37 45 30 24 39 1 18
[X3,] 39 49 7 36 14 43 26 24
[X4,] 45 31 26 37 12 47 37 15
[X5,] 23 27 34 29 30 37 17 4
[X6,] 9 46 39 34 8 37 42 37
Now each row of matrix V should be matched with each row of matrix X to get a count matrix like
[,V1] [,V2] [,V3] [,V4] [,V5] [,V6] [,V7] [,8]
[X1,] 7
[X2,]
To check the common numbers between X1 and V1??
How do I do it using R? Please suggest me some ideas
Here is one quick 'brute-force' way with apply
row.names(V) <- paste0("V",seq(6))
row.names(X) <- paste0("X",seq(6))
apply(V, 1, function(i){
apply(X, 1, function(j){
length(intersect(i, j))
}
)
})
V1 V2 V3 V4 V5 V6
X1 7 1 0 3 1 1
X2 2 6 2 2 1 2
X3 0 2 8 1 0 2
X4 3 2 1 7 0 1
X5 3 1 0 1 7 2
X6 1 1 1 1 1 7
Use == to compare the elements of the two matrices. This will give you a matrix of logicals (TRUEs and FALSEs). You can then add up the the number of TRUEs in each row using apply().
apply(V==X, 1, sum)
Related
I want to know the command in R to lag a matrix.
I have defined x as:
> (x <- matrix(1:50, 10, 5))
[,1] [,2] [,3] [,4] [,5]
[1,] 1 11 21 31 41
[2,] 2 12 22 32 42
[3,] 3 13 23 33 43
[4,] 4 14 24 34 44
[5,] 5 15 25 35 45
[6,] 6 16 26 36 46
[7,] 7 17 27 37 47
[8,] 8 18 28 38 48
[9,] 9 19 29 39 49
[10,] 10 20 30 40 50
I want create l.x:
[,1] [,2] [,3] [,4] [,5]
[1,] NA NA NA NA NA
[2,] 1 11 21 31 41
[3,] 2 12 22 32 42
[4,] 3 13 23 33 43
[5,] 4 14 24 34 44
[6,] 5 15 25 35 45
[7,] 6 16 26 36 46
[8,] 7 17 27 37 47
[9,] 8 18 28 38 48
[10,] 9 19 29 39 49
lag will coerce your object to a time-series (ts class to be specific) and only shifts the time index. It does not change the underlying data.
You need to manually lag the matrix yourself by adding rows of NA at the beginning and removing the same number of rows at the end. Here's an example of a function that does just that:
lagmatrix <- function(x, k) {
# ensure 'x' is a matrix
stopifnot(is.matrix(x))
if (k == 0)
return(x)
na <- matrix(NA, nrow=abs(k), ncol=ncol(x))
if (k > 0) {
nr <- nrow(x)
# prepend NA and remove rows from end
rbind(na, x[-((nr-k):nr),])
} else {
# append NA and remove rows from beginning
rbind(x[-1:k,], na)
}
}
Or you can use a lag function that does what you expect. For example, xts::lag.xts.
> xts::lag.xts(x)
[,1] [,2] [,3] [,4] [,5]
[1,] NA NA NA NA NA
[2,] 1 11 21 31 41
[3,] 2 12 22 32 42
[4,] 3 13 23 33 43
[5,] 4 14 24 34 44
[6,] 5 15 25 35 45
[7,] 6 16 26 36 46
[8,] 7 17 27 37 47
[9,] 8 18 28 38 48
[10,] 9 19 29 39 49
> is.matrix(xts::lag.xts(x))
[1] TRUE
Here is one manual method in base R with head and rbind:
rbind(NA, head(x, 9))
[,1] [,2] [,3] [,4] [,5]
[1,] NA NA NA NA NA
[2,] 1 11 21 31 41
[3,] 2 12 22 32 42
[4,] 3 13 23 33 43
[5,] 4 14 24 34 44
[6,] 5 15 25 35 45
[7,] 6 16 26 36 46
[8,] 7 17 27 37 47
[9,] 8 18 28 38 48
[10,] 9 19 29 39 49
More generally, as noted by #akrun, head(., -1) will work for any sized matrix:
rbind(NA, head(x, -1))
We can use apply
library(dplyr)
apply(x, 2, lag)
# [,1] [,2] [,3] [,4] [,5]
# [1,] NA NA NA NA NA
# [2,] 1 11 21 31 41
# [3,] 2 12 22 32 42
# [4,] 3 13 23 33 43
# [5,] 4 14 24 34 44
# [6,] 5 15 25 35 45
# [7,] 6 16 26 36 46
# [8,] 7 17 27 37 47
# [9,] 8 18 28 38 48
#[10,] 9 19 29 39 49
0r
rbind(NA, x[-nrow(x),])
# [,1] [,2] [,3] [,4] [,5]
# [1,] NA NA NA NA NA
# [2,] 1 11 21 31 41
# [3,] 2 12 22 32 42
# [4,] 3 13 23 33 43
# [5,] 4 14 24 34 44
# [6,] 5 15 25 35 45
# [7,] 6 16 26 36 46
# [8,] 7 17 27 37 47
# [9,] 8 18 28 38 48
#[10,] 9 19 29 39 49
Below is a pure dplyr solution without the need for apply. Only annoyance here is that it needs to be converted to a data.frame to work.
library(dplyr)
x %>% as.data.frame %>% mutate_each( funs(lag))
Matlab can do this task. I cannot get it right so far by using matrix(), t(), and reShape().
My intention is to transpose a series to a matrix of fixed 10 rows and the number of column varies based on the length of the data series. If these are some remains left, they can be discarded.
For example:
Row #1 1 2 3 4
Row #2 5 6 7 8
Row #3 9 10 11 12
Row #4 13 14 15 16
Row #5 17 18 19 20
Row #6 21 22 23 24
Row #7 25 26 27 28
Row #8 29 30 31 32
Row #9 33 34 35 36
Row #10 37 38 39 40
If there are any remains left (i.e, 41~49), these data can be just discarded.
Any suggestions?
This is what I think you are asking for. A vector of arbitrary length and data. To be turned into a matrix with nrow 10 and ncol based on data length.
#your series of arbitrary length
data = 1:49
#calculate number of columns based on length
col = as.integer(length(data)/10)
#max index
maxIndx = 10*col
#create and transpose matrix
yourMtx = t(matrix(data[0:maxIndx],col,10))
#your matrix
> [,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
[4,] 13 14 15 16
[5,] 17 18 19 20
[6,] 21 22 23 24
[7,] 25 26 27 28
[8,] 29 30 31 32
[9,] 33 34 35 36
[10,] 37 38 39 40
#create reverse matrix
revMtx = yourMtx[,rev(seq_len(ncol(yourMtx)))]
#reverse matrix
> [,1] [,2] [,3] [,4]
[1,] 4 3 2 1
[2,] 8 7 6 5
[3,] 12 11 10 9
[4,] 16 15 14 13
[5,] 20 19 18 17
[6,] 24 23 22 21
[7,] 28 27 26 25
[8,] 32 31 30 29
[9,] 36 35 34 33
[10,] 40 39 38 37
If I understand your question correctly, this looks to be an approach you could use.
# generate my series
myseries <- 1:49
# specify number of columns and rows
ncols <- 4
nrows <- 10
# create a matrix with the first ncols*nrows elements and fill by row
mymatrix <- matrix(myseries[1:(ncols*nrows)],
ncol = ncols, nrow = nrows, byrow = TRUE)
mymatrix
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
[4,] 13 14 15 16
[5,] 17 18 19 20
[6,] 21 22 23 24
[7,] 25 26 27 28
[8,] 29 30 31 32
[9,] 33 34 35 36
[10,] 37 38 39 40
In R, what is the fastest way(shortest code) to print multiplication table?
The functions seq rep and the bind functions help, but I'm looking for the shortest line(s) of code to do this.
rbind("1\'s"=1:12, "2\'s"=seq(2,24,2), "3\'s"=seq(3,36,3),
"4\'s"=seq(4,48,4), "5\'s"=seq(5,60,5), "6\'s"=seq(6,72,6))
Prints the 1's through 6's going across (horizontally). Anyone know how to perform this in a more compact way?
tbl <- outer(1:6, 1:12, "*")
rownames(tbl) <- paste(1:6, "'s", sep="")
tbl
You could make slightly more compact by using paste0(1:6, "'s")
This seems a slight improvement:
> v<-setNames(1:6, paste0(1:6, "\'s"))
> v %o% v
1's 2's 3's 4's 5's 6's
1's 1 2 3 4 5 6
2's 2 4 6 8 10 12
3's 3 6 9 12 15 18
4's 4 8 12 16 20 24
5's 5 10 15 20 25 30
6's 6 12 18 24 30 36
A shortcut for outer(1:6, 1:12, "*"):
> 1:6 %o% 1:12
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 1 2 3 4 5 6 7 8 9 10 11 12
[2,] 2 4 6 8 10 12 14 16 18 20 22 24
[3,] 3 6 9 12 15 18 21 24 27 30 33 36
[4,] 4 8 12 16 20 24 28 32 36 40 44 48
[5,] 5 10 15 20 25 30 35 40 45 50 55 60
[6,] 6 12 18 24 30 36 42 48 54 60 66 72
I have a 60 column matrix, and I want to reverse the some of its rows.
I came across the following two ways to do this:
#rtr is an integer vectors with the indices of the rows I want to reverse
matrix[rtr,]<-matrix[rtr,(ncol(matrix):1]
and
matrix[rtr,]<-rev(mat[rtr,])
Are these two implementations expected to produce the same result, or
are there some differences between them?
Thanks in advance
This seems to be a pretty easy thing to test
mm <- matrix(1:(6*7), ncol=6)
m2 <- m1 <- mm
rtr<-c(1,6,7)
m1[rtr,]<-m1[rtr, ncol(m1):1]
# [,1] [,2] [,3] [,4] [,5] [,6]
# [1,] 36 29 22 15 8 1
# [2,] 2 9 16 23 30 37
# [3,] 3 10 17 24 31 38
# [4,] 4 11 18 25 32 39
# [5,] 5 12 19 26 33 40
# [6,] 41 34 27 20 13 6
# [7,] 42 35 28 21 14 7
m2[rtr,]<-rev(m2[rtr,])
# [,1] [,2] [,3] [,4] [,5] [,6]
# [1,] 42 35 28 21 14 7
# [2,] 2 9 16 23 30 37
# [3,] 3 10 17 24 31 38
# [4,] 4 11 18 25 32 39
# [5,] 5 12 19 26 33 40
# [6,] 41 34 27 20 13 6
# [7,] 36 29 22 15 8 1
We can see they produce different output. The latter changes the order of the rows as well rather than just reversing them "in place"
In R, what is the fastest way(shortest code) to print multiplication table?
The functions seq rep and the bind functions help, but I'm looking for the shortest line(s) of code to do this.
rbind("1\'s"=1:12, "2\'s"=seq(2,24,2), "3\'s"=seq(3,36,3),
"4\'s"=seq(4,48,4), "5\'s"=seq(5,60,5), "6\'s"=seq(6,72,6))
Prints the 1's through 6's going across (horizontally). Anyone know how to perform this in a more compact way?
tbl <- outer(1:6, 1:12, "*")
rownames(tbl) <- paste(1:6, "'s", sep="")
tbl
You could make slightly more compact by using paste0(1:6, "'s")
This seems a slight improvement:
> v<-setNames(1:6, paste0(1:6, "\'s"))
> v %o% v
1's 2's 3's 4's 5's 6's
1's 1 2 3 4 5 6
2's 2 4 6 8 10 12
3's 3 6 9 12 15 18
4's 4 8 12 16 20 24
5's 5 10 15 20 25 30
6's 6 12 18 24 30 36
A shortcut for outer(1:6, 1:12, "*"):
> 1:6 %o% 1:12
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 1 2 3 4 5 6 7 8 9 10 11 12
[2,] 2 4 6 8 10 12 14 16 18 20 22 24
[3,] 3 6 9 12 15 18 21 24 27 30 33 36
[4,] 4 8 12 16 20 24 28 32 36 40 44 48
[5,] 5 10 15 20 25 30 35 40 45 50 55 60
[6,] 6 12 18 24 30 36 42 48 54 60 66 72