Suppose I have a matrix similar as the one show below in R.
[,1] [,2] [,3]
[1,] 2 4 3
[2,] 2 5 7
How can append a column to the front like below.
[,1] [,2] [,3] [,4]
[1,] 1 2 4 3
[2,] 1 1 5 7
Lastly, the matrix has many rows.
use cbind
cbind(c(1,2), matrix(1:6, nrow=2))
So in case you work with bigger data, imagine your matrix is saved as m and you have a vector my_vector you want to add as a column in front of this matrix, the command would be
new_m <- cbind(my_vector, m)
Make sure the dimension of your vector fit the number of rows in your matrix.
In case you want to add rows instead of columns, the command is called rbind and is used in exactly the same way.
Related
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.
For example, I have a 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
I want it to become
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 1 2 3 4 5 6 7 8
[2,] 9 10 11 12 13 14 15 16
Thanks.
Let me expand on Zheyuan Li's answer, since these things can be a bit mysterious for the uninitiated. Basically, the same matrix function used to create a matrix from a vector, can also be used to reshape a matrix.
All one needs to realize is that a matrix is much like a vector but with a $dim attribute for its shape, and that the values of that underlying vector are stored by column.
To create your original matrix, you could do:
A <- matrix(1:16, nrow=4, byrow=TRUE)
print(attributes(A))
The byrow argument tells matrix to allocate the elements of the input vector in a rowwise fashion to the matrix, instead of columnwise.
However, it does not change the fact that after this allocation, the internal storing of values in the matrix is still by column. The byrow argument has then simply changed the ordering of elements in the underlying vector, as can easily be seen:
print(as.numeric(A))
What we need to get your desired output, is to first get the sequence in your matrix ordered by column - so that the underlying vector is 1:16 again. For this we can use the transpose function t(). After the transpose, we can bring the now nicely ordered values into the desired 2x8 shape in a rowwise fashion. So:
B <- matrix(t(A), nrow=2, byrow=TRUE)
print(B)
My aim is to delete specific positions in a matrix according to a vector. Just giving you a small example.
Users_pos <- c(1,2)
Items_pos <- c(3,2)
Given a Matrix A:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
My aim according to the two Vectors User_pos and Item_pos is to delete the following values
A[1,3] and A[3,2]
I'm wondering if there's a possibility to do so without typing in the values for rows and columns by hand.
You can index k elements in a matrix A using A[X], where X is a k-row, 2-column matrix where each row is the (row, col) value of the indicated element. Therefore, you can index your two elements in A with the following indexing matrix:
rbind(Users_pos, Items_pos)
# [,1] [,2]
# Users_pos 1 2
# Items_pos 3 2
Using this indexing, you could choose to extract the information current stored with A[X] or replace those elements with A[X] <- new.values. If you, for instance, wanted to replace these elements with NA, you could do:
A[rbind(Users_pos, Items_pos)] <- NA
A
# [,1] [,2] [,3]
# [1,] 1 NA 3
# [2,] 4 5 6
# [3,] 7 NA 9
How do I load a column of a matrix in R as a variable? After I do that I want to access individual row elements in that variable created in a for loop, how do I access that? The matrix itself is only two dimensional.
use [
A sample matrix:
> mat=matrix(1:6, 2)
> mat
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
Extract column 2:
> mat[,2]
[1] 3 4
What are you actually trying to do? for is almost always the wrong approach.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
generate variable names (something like get())
If I want to create matrices with different names in an automatized way, I run into problems.
For example, I want to create three matrices named a1,a2 and a3.
x<-1:3
a<-"a"
listofnames<-paste(a,x) ## a vector with the names that I want to use for my matrices
My problem is to assign a matrix the different names from the vector I created.
For example, to create a matrix called a1 (the first "name" in my vector), this will of course not work at all:
listofnames[1]<-matrix(ncol=2,nrow=2)
But how would I do it?
I've been looking on the internet but can't find any answer..
Thank you so much for your help
Use assign as in:
x<-1:3
a<-"a"
listofnames <-paste(a,x)
set.seed(001)
for(i in 1:length(listofnames)){
assign(listofnames[i], matrix(sample(9), ncol=3))
}
get(listofnames[1])
[,1] [,2] [,3]
[1,] 3 6 8
[2,] 9 2 7
[3,] 5 4 1
get(listofnames[2])
[,1] [,2] [,3]
[1,] 1 5 6
[2,] 2 7 3
[3,] 8 4 9
get(listofnames[3])
[,1] [,2] [,3]
[1,] 4 2 5
[2,] 7 9 3
[3,] 8 1 6
Once you assign matrices to the names contained in listofnames you can access by using get function as shown above. If you only do listofnames[1] this will give you the firt name in listofnames but not the elements stored under that name, to do so you must use get(listofnames[1])
It might be better if you explain exactly what you are trying to achieve, but you might also want to explore assign():
x <- 1:3
a <- "a"
listofnames <- paste(a, x, sep="")
assign(listofnames[1], matrix(nrow = 2, ncol = 2))
a1
[,1] [,2]
[1,] NA NA
[2,] NA NA