R. how to make matrix which have equal rows - r

I have row f. I want create matrix R such that every row of it is equal f.
What is the most efficient way to do it in R?

with a row
f=c(1,22,33,44,55,66)
get its length
lf=length(f)
Then make the matrix
R=matrix(rep(f,lf),
ncol=lf,
byrow=T)
Gives:
R
[,1] [,2] [,3] [,4] [,5]
[1,] 1 33 44 55 66
[2,] 1 33 44 55 66
[3,] 1 33 44 55 66
[4,] 1 33 44 55 66
[5,] 1 33 44 55 66

R <- matrix(f, 1)[rep(1,n), ]
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 1 2 3 4 5
[3,] 1 2 3 4 5
[4,] 1 2 3 4 5
[5,] 1 2 3 4 5
Or even more compact:
R <- rbind(f)[rep(1,n), ]
[,1] [,2] [,3] [,4] [,5]
f 1 2 3 4 5
f 1 2 3 4 5
f 1 2 3 4 5
f 1 2 3 4 5
f 1 2 3 4 5
Note that rownames of matrices do not need to be unique, unlike the case with data.frames.

Here is one possibility:
mymat <- do.call( rbind, rep(list(f), 10) )

Related

Switch rows between two matrices in R

Let the matrices A and B.
A=c(1:5)*matrix(1,5,5)
B=10*A
that is,
> A
[,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
> B
[,1] [,2] [,3] [,4] [,5]
[1,] 10 10 10 10 10
[2,] 20 20 20 20 20
[3,] 30 30 30 30 30
[4,] 40 40 40 40 40
[5,] 50 50 50 50 50
I would like, for example, to switch the first rows between the matrices A and B, that is
> A
[,1] [,2] [,3] [,4] [,5]
[1,] 10 10 10 10 10
[2,] 2 2 2 2 2
[3,] 3 3 3 3 3
[4,] 4 4 4 4 4
[5,] 5 5 5 5 5
> B
[,1] [,2] [,3] [,4] [,5]
[1,] 1 1 1 1 1
[2,] 20 20 20 20 20
[3,] 30 30 30 30 30
[4,] 40 40 40 40 40
[5,] 50 50 50 50 50
using a function, and without using any indermediate vector or a for loop.
Update
As per you update in the comment, you can try
lapply(
1:nrow(B),
function(k) {
setNames(
Map(
function(x, ind,r) {
x[ind, ] <- r
x
},
list(A, B),
list(1,k),
list(B[k, ], A[1, ])
), c("A", "B")
)
}
)
which gives
[[1]]
[[1]]$A
[,1] [,2] [,3] [,4] [,5]
[1,] 10 10 10 10 10
[2,] 2 2 2 2 2
[3,] 3 3 3 3 3
[4,] 4 4 4 4 4
[5,] 5 5 5 5 5
[[1]]$B
[,1] [,2] [,3] [,4] [,5]
[1,] 1 1 1 1 1
[2,] 20 20 20 20 20
[3,] 30 30 30 30 30
[4,] 40 40 40 40 40
[5,] 50 50 50 50 50
[[2]]
[[2]]$A
[,1] [,2] [,3] [,4] [,5]
[1,] 20 20 20 20 20
[2,] 2 2 2 2 2
[3,] 3 3 3 3 3
[4,] 4 4 4 4 4
[5,] 5 5 5 5 5
[[2]]$B
[,1] [,2] [,3] [,4] [,5]
[1,] 10 10 10 10 10
[2,] 1 1 1 1 1
[3,] 30 30 30 30 30
[4,] 40 40 40 40 40
[5,] 50 50 50 50 50
[[3]]
[[3]]$A
[,1] [,2] [,3] [,4] [,5]
[1,] 30 30 30 30 30
[2,] 2 2 2 2 2
[3,] 3 3 3 3 3
[4,] 4 4 4 4 4
[5,] 5 5 5 5 5
[[3]]$B
[,1] [,2] [,3] [,4] [,5]
[1,] 10 10 10 10 10
[2,] 20 20 20 20 20
[3,] 1 1 1 1 1
[4,] 40 40 40 40 40
[5,] 50 50 50 50 50
[[4]]
[[4]]$A
[,1] [,2] [,3] [,4] [,5]
[1,] 40 40 40 40 40
[2,] 2 2 2 2 2
[3,] 3 3 3 3 3
[4,] 4 4 4 4 4
[5,] 5 5 5 5 5
[[4]]$B
[,1] [,2] [,3] [,4] [,5]
[1,] 10 10 10 10 10
[2,] 20 20 20 20 20
[3,] 30 30 30 30 30
[4,] 1 1 1 1 1
[5,] 50 50 50 50 50
[[5]]
[[5]]$A
[,1] [,2] [,3] [,4] [,5]
[1,] 50 50 50 50 50
[2,] 2 2 2 2 2
[3,] 3 3 3 3 3
[4,] 4 4 4 4 4
[5,] 5 5 5 5 5
[[5]]$B
[,1] [,2] [,3] [,4] [,5]
[1,] 10 10 10 10 10
[2,] 20 20 20 20 20
[3,] 30 30 30 30 30
[4,] 40 40 40 40 40
[5,] 1 1 1 1 1
You can try the code below
list2env(
setNames(
Map(
function(x, r) {
x[1, ] <- r
x
},
list(A, B),
list(B[1, ], A[1, ])
), c("A", "B")
),
envir = .GlobalEnv
)
replace seems to work without any intermediate
replace(A, cbind(1, 1:ncol(A)), B[1,])
replace(B, cbind(1, 1:ncol(A)), A[1,])
Note that once we do the assignment to the original object, the second assignment is not possible as the original object is changed
A clean way to swap is to create a temporary object and rm it
tmp <- A[1,]
A[1, ] <- B[1, ]
B[1, ] <- tmp
rm(tmp)
gc()
Or probably create a function, and do the swap inside the function, thus the activation record is deleted once it exit the function (as these are pass by value)
f1 <- function(a, b) {
t1 <- a[1,]
a[1,] <- b[1,]
b[1,] <- t1
return(list(a, b))
}
list2env(setNames(f1(A, B), c('A', 'B')), .GlobalEnv)

create a Vandermonde matrix in R

I am new to R and still learning it. I want to m-by-n Vandermonde matrix
I know it can be done via for loops to assign values to corresponding indices within the matrix, but it seems inefficient when m or n is large. I need some advices to have a simpler and efficient way to generate the Vandermonde matrix? Thanks for help in advance!
Use outer like this:
n <- 6; alpha <- 1:5 # test data
outer(alpha, seq(0, n-1), `^`)
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 1 1 1 1 1
## [2,] 1 2 4 8 16 32
## [3,] 1 3 9 27 81 243
## [4,] 1 4 16 64 256 1024
## [5,] 1 5 25 125 625 3125
A base R solution is to define your custom function vander, where sapply + cumprod are used
vander <- function(alpha,n) t(sapply(alpha, function(k) c(1,cumprod(rep(k,n-1)))))
vm1 <- vander(alpha,n)
Another option is from package matrixcalc, in which vandermonde.matrix can make it
vm2 <- matrixcalc::vandermonde.matrix(alpha,n)
Example
Given alpha and n like below
alpha <- 1:4
n <- 5
then you will get
> vm1
[,1] [,2] [,3] [,4] [,5]
[1,] 1 1 1 1 1
[2,] 1 2 4 8 16
[3,] 1 3 9 27 81
[4,] 1 4 16 64 256
> vm2
[,1] [,2] [,3] [,4] [,5]
[1,] 1 1 1 1 1
[2,] 1 2 4 8 16
[3,] 1 3 9 27 81
[4,] 1 4 16 64 256

How can I make the matrix as in the following image in R?

I know that I can make a simple matrix (for example 1->20 numbers, 4,5 rows) using this:
x<- array(1:20, dim=c(4,5)); x
But I have no idea how to make a matrix similar to the one on the picture...
matrix(rep(1:4,4)^rep(0:3,each=4), byrow=TRUE, nrow=4)
[,1] [,2] [,3] [,4]
[1,] 1 1 1 1
[2,] 1 2 3 4
[3,] 1 4 9 16
[4,] 1 8 27 64
You can create a function if you want to be able to generate an analogous square matrix with an arbitrary number of rows.
power_mat = function(n) {
matrix(rep(1:n,n)^rep(1:n - 1, each=n), byrow=TRUE, nrow=n)
}
power_mat(6)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 1 1 1 1 1
[2,] 1 2 3 4 5 6
[3,] 1 4 9 16 25 36
[4,] 1 8 27 64 125 216
[5,] 1 16 81 256 625 1296
[6,] 1 32 243 1024 3125 7776
Solution
t(sapply(0:3, function(x) (1:4)^x))
Result
[,1] [,2] [,3] [,4]
[1,] 1 1 1 1
[2,] 1 2 3 4
[3,] 1 4 9 16
[4,] 1 8 27 64
(You can easily verify this matches your matrix)
Explanation
Each row of the matrix is the vector c(1, 2, 3, 4) raised to the power of the row index (in a 0-based indexing system). So, we just use sapply() on the vector of powers to raise 1:4 to each power which will return a matrix. Because sapply() would return the results as column vectors, we use t() to transpose the result.
n <- 4
m <- 4
t(sapply( 0:(n-1), FUN = function (i){ return(c(1:m)**i) }))
where n is the number of rows, and m is the number of columns.
[,1] [,2] [,3] [,4]
[1,] 1 1 1 1
[2,] 1 2 3 4
[3,] 1 4 9 16
[4,] 1 8 27 64
n = 4
m = rbind(rep(1, n), t(replicate(n-1, 1:n)))
m ^ (row(m) - 1)
# [,1] [,2] [,3] [,4]
#[1,] 1 1 1 1
#[2,] 1 2 3 4
#[3,] 1 4 9 16
#[4,] 1 8 27 64

How do I understand this index matrix in R

Here I have a
x <- array(1:20, dim=c(4,5))
x
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 9 13 17
[2,] 2 6 10 14 18
[3,] 3 7 11 15 19
[4,] 4 8 12 16 20
I also have a index array
i <- array(c(1:3,3:1), dim=c(3,2))
[,1] [,2]
[1,] 1 3
[2,] 2 2
[3,] 3 1
Then
x[i]
will extract X[1,3], X[2,2] and X[3,1]
However, what if I have
i <- array(c(1:3,3:1), dim=c(2,3))
The output is
x[i]
[1] 1 2 3 3 2 1
How can I understand this result?

Is there any way to sort columns of a matrix independently in R?

I'm a newbie in R so, I really need some help here. I just want to sort each column independently. Any help is appreciated!
> mat <- matrix(c(45,34,1,3,4325,23,1,2,5,7,3,4,32,734,2),ncol=3)
> mat
[,1] [,2] [,3]
[1,] 45 23 3
[2,] 34 1 4
[3,] 1 2 32
[4,] 3 5 734
[5,] 4325 7 2
to
[,1] [,2] [,3]
[1,] 1 1 2
[2,] 3 2 3
[3,] 34 5 4
[4,] 45 7 32
[5,] 4325 23 734
Yes, there is!
apply(mat, 2, sort)
[,1] [,2] [,3]
[1,] 1 1 2
[2,] 3 2 3
[3,] 34 5 4
[4,] 45 7 32
[5,] 4325 23 734

Resources