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
Related
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
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
I am looking for a clever way to force the dimensions (both nrow and ncol) of a matrix to be even without using an if statement. By force I mean subtract the first appropriate column and/or row so that both dimensions are even.
I was hoping something like this would work:
## build a matrix with odd number of columns and even number of rows
x=matrix(1:12,nrow=4,ncol=3)
## we can check which (if any) dimensions are odd with
dim(x) %% 2 ## 0,1
## I would like get a matrix that looks like
[,1] [,2]
[1,] 5 9
[2,] 6 10
[3,] 7 11
[4,] 8 12
## By using something similar to
x.even = x[-nrow(x)%%2,-ncol(x)%%2]
Obviously the last line does not give the desired result. Is there a clever way to do this without using a conditional?
Just divide nrow and ncol by 2, take floor, and multiply by 2 again
x.even = x[1:(2*floor(nrow(x)/2)),1:(2*floor(ncol(x)/2))]
One way that builds on your solution:
#start rows and columns from 1
#also subtract remainder from total rows and columns
x[1:(nrow(x) - nrow(x) %% 2), 1:(ncol(x) - ncol(x) %% 2)]
output:
[,1] [,2]
[1,] 1 5
[2,] 2 6
[3,] 3 7
[4,] 4 8
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
This is a really basic question, but I can't seem to solve it or find an answer for it anywhere : suppose I have two vectors x,y of coordinates and a matrix m.
I would like a vector z such that z[i] = m[x[i],y[i]]for all i.
I tried z=m[x,y], but that creates a memory overflow. The vector and matrix are quite large so looping is pretty much out of the question. Any ideas ?
Use cbind. Here's a simple example:
mat <- matrix(1:25, ncol = 5)
mat
# [,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
x <- 1:5
y <- c(2, 3, 1, 4, 3)
mat[cbind(x, y)]
# [1] 6 12 3 19 15
## Verify with a few values...
mat[1, 2]
# [1] 6
mat[2, 3]
# [1] 12
mat[3, 1]
# [1] 3
From ?Extract:
A third form of indexing is via a numeric matrix with the one column for each dimension: each row of the index matrix then selects a single element of the array, and the result is a vector. Negative indices are not allowed in the index matrix. NA and zero values are allowed: rows of an index matrix containing a zero are ignored, whereas rows containing an NA produce an NA in the result.
Another way is to use the fact that you can index a matrix as if it were a vector, with elements numbered in column-major form. Using the example from #AnandoMahto:
mat[x+nrow(mat)*(y-1)]
[1] 6 12 3 19 15