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
Related
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)
I just spent last week learning R, and now I am playing with it, but I can't find the answer for this problem
I have utility function written like this
u(x,y)=min(3x,9y)
and the goal is to plot a contour graph of this function.
I tried quite a lot of solutions, until now I came to this
x<-seq(0,30,3)
y<-seq(0,90,9)
n<-1:11
table<-c(
pmin(x,y[1]),
pmin(x,y[2]),
pmin(x,y[3]),
pmin(x,y[4]),
pmin(x,y[5]),
pmin(x,y[6]),
pmin(x,y[7]),
pmin(x,y[8]),
pmin(x,y[9]),
pmin(x,y[10]),
pmin(x,y[11]))
mat<-matrix(table, ncol=11, nrow=11)
contour(x,y,mat)
and obviously, the contour graph is not very precise.
I would like to now, what can I use so I do not have to write the table by hand like this.
I wanted to use the sapply function somehow, but I have two values and I was kind of lost how to put them there.
and I would be very thankful, if someone showed me, how to plot this contour graph as most efficiently as possible.
You can use outer for this:
n <- 11
foo <- outer(X=seq_len(n), Y=seq_len(n), function(x, y) pmin(3*x, 9*y))
outer applies the specified function to all combinations of elements of the vectors passed to arguments X and Y. In this case, we're applying the function pmin(3*x, 9*y) to all pairs of elements of the two vectors, each of which are the numbers 1 to n.
foo
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
## [1,] 3 3 3 3 3 3 3 3 3 3 3
## [2,] 6 6 6 6 6 6 6 6 6 6 6
## [3,] 9 9 9 9 9 9 9 9 9 9 9
## [4,] 9 12 12 12 12 12 12 12 12 12 12
## [5,] 9 15 15 15 15 15 15 15 15 15 15
## [6,] 9 18 18 18 18 18 18 18 18 18 18
## [7,] 9 18 21 21 21 21 21 21 21 21 21
## [8,] 9 18 24 24 24 24 24 24 24 24 24
## [9,] 9 18 27 27 27 27 27 27 27 27 27
## [10,] 9 18 27 30 30 30 30 30 30 30 30
## [11,] 9 18 27 33 33 33 33 33 33 33 33
contour(foo)
To increase the number of points at which the function is evaluated, just pass finer-resolution vectors:
foo2 <- outer(seq(1, 11, 0.01), seq(1, 11, 0.01), function(x, y) pmin(3*x, 9*y))
contour(foo2)
Now it is evaluated at 0.01 increments from 1 through 11.
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"
Imagine we have one matrix of 5*5 (25 elements)
m<-matrix(1:25,5,5)
> m
[,1] [,2] [,3] [,4] [,5]
[1,] 1 6 11 16 21
[2,] 2 7 12 17 22
[3,] 3 8 13 18 23
[4,] 4 9 14 19 24
[5,] 5 10 15 20 25
I want to produce large matrix with different dimensions from the matrix “m”
For example 5*8, now my second column of new matrix must have overlap with the first one and so on.
So bigger dimension (e.g 5*8) matrix would be like : (it is just example and not sure the amount of shift is correct)
[,1] [,2] [,3] [,4] ……………………[,8]
[1,] 1 4 7 10 …………………… 19
[2,] 2 5 8 11 …………………… 20
[3,] 3 6 9 12 …………………… 21
[4,] 4 7 10 13 …………………… 22
[5,] 5 8 11 14 …………………… 23
In fact in each column we have a shift back to some elements of last column in order to prevent from reaching the last element of original matrix and producing NA value.
Please anyone knows how to create such a larger matrix?
The hardest part for me is to calculate the amount of SHIFT value regarding to the size of larger matrix. The larger matrix must cover almost all elements of the original one. (it is ok to miss some last elements)
thanks
I'm not sure what you want, but this might be helpful.
rows <- 5
cols <- 8
overlap <- 1
matrix(rep(seq(1,cols)*(rows-overlap),each=rows)+seq(1,rows)-(rows-overlap),nrow=rows)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 1 5 9 13 17 21 25 29
[2,] 2 6 10 14 18 22 26 30
[3,] 3 7 11 15 19 23 27 31
[4,] 4 8 12 16 20 24 28 32
[5,] 5 9 13 17 21 25 29 33
overlap <- 2
matrix(rep(seq(1,cols)*(rows-overlap),each=rows)+seq(1,rows)-(rows-overlap),nrow=rows)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 1 4 7 10 13 16 19 22
[2,] 2 5 8 11 14 17 20 23
[3,] 3 6 9 12 15 18 21 24
[4,] 4 7 10 13 16 19 22 25
[5,] 5 8 11 14 17 20 23 26
overlap <- 3
matrix(rep(seq(1,cols)*(rows-overlap),each=rows)+seq(1,rows)-(rows-overlap),nrow=rows)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 1 3 5 7 9 11 13 15
[2,] 2 4 6 8 10 12 14 16
[3,] 3 5 7 9 11 13 15 17
[4,] 4 6 8 10 12 14 16 18
[5,] 5 7 9 11 13 15 17 19
the maximum efficiency of coverage is: (the best value of overlap)
> overlap<-ceiling(rows*(1-length(dna1)/(cols*rows)))+round(rows/cols)