Raising the entries of a matrix by column - r

I need to create a function (x,k), were x= vector of length n, and k=integer.
The function needs to give me a matrix of dimensions [n x k], and the entries in each column need to be raised to the power of the number of that column (ie. in column one the entries are x, in column 2 the entries are x^2, etc).
I'm having a hard time figuring it out how to structure a function that would do this type of operation by column.
Thank you so much.

Something like this probably, taking advantage of outer, which returns a matrix as a result of applying a function to the two vectors.
matpower <- function(x,k) outer(x,seq_len(k),`^`)
matpower(1:4,4)
# [,1] [,2] [,3] [,4]
#[1,] 1 1 1 1
#[2,] 2 4 8 16
#[3,] 3 9 27 81
#[4,] 4 16 64 256

Related

How to update matrix value in R based on comparison of two matrix?

How to generate a matrix based on a comparison of two matrices. I have (column,row) matrix A (10,1) and B (10, 100). Matrix A is compared to each row of matrix B if the value of B is smaller than A then value B is updated to a value of A.
n.units<-100
n.option<-10
A<-rnorm(n.option,1,0.2)
B<-matrix(rnorm(n.option*n.units,1,0.2)n.col=n.units)
renew <-function(){Thresholds=obj.value }
update1 <- apply((Thresholds < obj.value),1,renew)
I am new to R programming, please give some advice to solve it.
I guess what you are trying can be achieved with pmax. Try
pmax(B, A)
You have a numeric vector A which is compared with matrix B. 1st element of A is compared with first row of B and the maximum value is selected. Same goes for all other rows since pmax recycles the shorter vector to the longer vector length. Also note that pmax(B, A) gives different structure than pmax(A, B) although the value is the same.
Just to make it easier to understand, consider this example
mat <- matrix(1:10, ncol = 5)
mat
# [,1] [,2] [,3] [,4] [,5]
#[1,] 1 3 5 7 9
#[2,] 2 4 6 8 10
pmax(mat, c(3, 7))
# [,1] [,2] [,3] [,4] [,5]
#[1,] 3 3 5 7 9
#[2,] 7 7 7 8 10
Here 1st row is compared with 3 and second row is compared with 7 and maximum value is selected.

R - expand.grid with dataset having columns of different sizes

I want to produce a matrix which holds all possible combinations of a vector x of integers from 1 to the respective number.
The length of the vector x may change.
For this sample vector:
x = c(3,8,2)
I want the result to look something like this:
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 1 1 2
[3,] 1 2 1
...
[48,] 3 8 2
I understand expand.grid does the job, however, I can't seem to find the parameters which allow for different sets in each column.
We get the sequence of each element (seq) and do expand.grid
out <- expand.grid(lapply(x, seq))
dim(out)
#[1] 48 3

R: How to convert a vector into matrix without replicating the vector?

Here's my problem:
I have a vector and I want to convert it into a matrix with fixed number of columns, but I don't want to replicate the vector to fill the matrix when it's necessary.
For example:
My vector has a length of 15, and I want a matrix with 4 columns.I wish to get the matrix wit 15 elements from the vector and a 0 for the last element in the matrix.
How can I do this?
Edit:
Sorry for not stating the question clearly and misguiding you guys with my example. In my program,I don't know the length of my vector, it depends on other parameters and this question involves with a loop, so I need a general solution that can solve many different cases, not just my example.
Thanks for answering.
You could subset your vector to a multiple of the number of columns (so as to include all the elements). This will add necessary amount of NA to the vector. Then convert to matrix.
x = 1:15
matrix(x[1:(4 * ceiling(length(x)/4))], ncol = 4)
# [,1] [,2] [,3] [,4]
#[1,] 1 5 9 13
#[2,] 2 6 10 14
#[3,] 3 7 11 15
#[4,] 4 8 12 NA
If you want to replace NA with 0, you can do so using is.na() in another step
We can also do this with dim<- and length<-
n <- 4
n1 <- ceiling(length(x)/n)
`dim<-`(`length<-`(x, n*n1), c(n1, n))
# [,1] [,2] [,3] [,4]
#[1,] 1 5 9 13
#[2,] 2 6 10 14
#[3,] 3 7 11 15
#[4,] 4 8 12 NA
data
x <- 1:15

how to find a triangular matrix by permutation

I have a square matrix which I know can become a triangular one by permutation, what would be the Matrix::function call to get the triangular one ?
I have thus B as
B <- matrix(c(0,5,6,1,2,3,0,0,9),3,3)
and want to get a function that renders as result
B[,c(2,1,3)]
You could try with
B[, order(sapply(1:ncol(B), function(x) rle(B[,x]==0)$lengths[1]*rle(B[,x]==0)$values[1]))]
# [,1] [,2] [,3]
#[1,] 1 0 0
#[2,] 2 5 0
#[3,] 3 6 9
This orders the columns of the matrix according to the number of consecutive zeros (counted from the top row downwards), with the shortest sequence of zeros first.

Vector calculaitons in R - troubles

I have some troubles in calculations in R. I have a vector of few numbers, and one sequence of numbers (vector as well, i think). Now i need to power all numbers of first vector on first element of second vector, sum that numbers and go on with each element of the second vector. So my result would be a vector of the same number of elements as the second vector. But i dont know how to program this equation. This is just the first part of my calculations, but i thing i can solve the rest by myself.
Thanks for reply!
Like this?
x <- 1:5
y <- 1:3
res <- outer(x, y, "^")
# [,1] [,2] [,3]
# [1,] 1 1 1
# [2,] 2 4 8
# [3,] 3 9 27
# [4,] 4 16 64
# [5,] 5 25 125
colSums(res)
#[1] 15 55 225

Resources