Consider the following matrix:
mat <- cbind(c(5,2,5),c(6,3,2))
I want to sum the two first rows, so I get the following matrix:
7 9
5 2
How do I do that in R?
You should use rowsum:
> rowsum(mat, c(1,1,2))
[,1] [,2]
1 7 9
2 5 2
The first argument is your matrix mat, the second one specifies how the rows should be grouped together. Here c(1,1,2) specify that first two rows are in one group (and summed together) and the third row is in another group.
Note: Do not confuse this with rowSums - a different function.
We can use colSums to sum first n rows and rbind remaining ones
n <- 2
rbind(colSums(mat[seq_len(n), ]), mat[(n + 1):nrow(mat), ])
# [,1] [,2]
#[1,] 7 9
#[2,] 5 2
Related
I have two matrices. I need to find the coefficient alpha of the elements in the second matrix based on whether a participant meets a condition in the first matrix. For example, for all the elements that are 1 in the first matrix:
[1,1] #participant 1 in 2008
[3,1] #participant 3 in 2008
[2,2] #participant 2 in 2009
[3,2] #participant 3 in 2009
I need to find the coefficient alpha for the all corresponding elements in matrix 2 or in this example, one coefficient alpha of all of the following elements:
[1,1:3] #2008 elements for participant 1
[3,1:3] #2008 elements for participant 3
[2,4:6] #2009 elements for participant 2
[3,4:6] #2009 elements for participant 3
my_vector_1<-c(1,2,1,4,1,1,7,8,2,4,5,6,10,11,2)
my_matrix_1<-matrix(data=my_vector_1, nrow=3, ncol=5)
my_matrix_1
colnames(my_matrix_1)<-c(paste0("Y", 2008:2012))
rownames(my_matrix_1)<-c(paste0("Participant", 1:3))
my_vector_2<-c(2,4,6,8,10,11,12,13,14,16,2,12,3,14,5,12,7,18,9,22,12,13,14,15,8,9,10)
my_matrix_2<-matrix(data=my_vector_2, nrow=3, ncol=9)
colnames(my_matrix_2)<-c("O2008", "Q2008", "R2008", "O2009", "Q2009", "R2009", "O2010", "Q2010", "R2010")
rownames(my_matrix_2)<-c(paste0("Participant", 1:3))
my_matrix_2
We could first find out all the row/column position where my_matrix_1 == 1. Use row index to subset row from my_matrix_2 and manipulate column index to get 3 column from each.
n <- 3
mat <- which(my_matrix_1 == 1, arr.ind = TRUE)
t(apply(mat, 1, function(x) my_matrix_2[x[1], (n * (x[2] - 1) + 1) : (n * x[2])]))
# [,1] [,2] [,3]
#Participant1 2 8 12
#Participant3 6 11 14
#Participant2 2 14 7
#Participant3 12 5 18
I understand what rowsum() does, but I'm trying to get it to work for myself. I've used the example provided in R which is structured as such:
x <- matrix(runif(100), ncol = 5)
group <- sample(1:8, 20, TRUE)
xsum <- rowsum(x, group)
What is the matrix of values that is produced by xsum and how are the values obtained. What I thought was happening was that the values obtained from group were going to be used to state how many entries from the matrix to use in a rowsum. For example, say that group = (2,4,3,1,5). What I thought this would mean is that the first two entries going by row would be selected as the first entry to xsum. It appears as though this is not what is happening.
rowsum adds all rows that have the same group value. Let us take a simpler example.
m <- cbind(1:4, 5:8)
m
## [,1] [,2]
## [1,] 1 5
## [2,] 2 6
## [3,] 3 7
## [4,] 4 8
group <- c(1, 1, 2, 2)
rowsum(m, group)
## [,1] [,2]
## 1 3 11
## 2 7 15
Since the first two rows correspond to group 1 and the last 2 rows to group 2 it sums the first two rows giving the first row of the output and it sums the last 2 rows giving the second row of the output.
rbind(`1` = m[1, ] + m[2, ], `2` = m[3, ] + m[4, ])
## [,1] [,2]
## 1 3 11
## 2 7 15
That is the 3 is formed by adding the 1 from row 1 of m and the 2 of row 2 of m. The 11 is formed by adding 5 from row 1 of m and 6 from row 2 of m.
7 and 15 are formed similarly.
I hope the title isn't too confusing...
Basically, I have two vectors that is each of n length. I want to transmute these two vectors to a n*n matrix (i.e. 2 vectors that contains 2 numbers each becomes a 2*2 matrix), where each position in the matrix is the median of each position of the two vectors.
For example:
a<-as.vector(1,5)
b<-as.vector(1,5)
Using outer() gives me a 2*2 matrix
1 5
1
5
But, how do I fill the empty matrix with median values between each unique combination? The answer should look something like this:
1 3
3 5
Try
outer(a, b, FUN= Vectorize(function(x,y) median(c(x,y))))
# [,1] [,2]
#[1,] 1 3
#[2,] 3 5
data
a <- c(1,5)
b <- a
I'd like to utilise one of the apply set of functions to do some calculations.
First off, I have two matrices, mat1 and mat2:
mat1:
a b c
1 NA NA NA
2 1 1 1
3 1 1 NA
4 NA 1 NA
mat2:
a b c
a 1.0 0.2 0.3
b -0.7 1.0 0.8
c -0.1 -0.3 1.0
mat2 is calculated using mat1 using a function which is irrelevant here, and essentially I'd like to apply a weighting function to mat1 which penalizes the results from mat2 when there is less data (and therefore less accurate).
So to achieve this, I want to, for some coordinate x,y in mat2, calculate the pairwise completeness of two columns of mat1.
For example: mat2["a","b"] or mat2["b","a"] (should be same) would become the original values * (the complete rows of mat1 of a and b/total rows of mat1 of a and b).
So really the question is how can I apply a function to a matrix that loops every column for every column (double loop) and store this in a weight matrix to multiply against another matrix?
I can already compare two rows using rollapply from zoo package like so:
rowSums(rollapply(is.na(t(mat1)), 2, function(x) !any(x)))
I get:
[1] 2 1
As in, comparing a and b, 2 rows are complete and comparing b and c, 1 row is complete. So how can I compare a to b, a to c and b to c?
Thanks.
I was looking at your question again, and it appears that you want a matrix X with the same dimensions of mat2, where X[i,j] is given by the number of complete cases in mat1[,c(i,j)]. Then mat2 will be multiplied by X.
The number of complete cases is given by sum(complete.cases(mat1[,c(i,j)])). I want to use this in outer which requires a vectorized function, so this is passed through Vectorize:
outer(seq(nrow(mat2)), seq(ncol(mat2)),
Vectorize(function(x,y) sum(complete.cases(mat1[,c(x,y)])))
)
## [,1] [,2] [,3]
## [1,] 2 2 1
## [2,] 2 3 1
## [3,] 1 1 1
This is your desired symmetric matrix.
I would like an out of bounds subscript on a matrix in R to return NAs instead of an error, like it does on vectors.
> a <- 1:3
> a[1:4]
[1] 1 2 3 NA
> b <- matrix(1:9, 3, 3)
> b
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> b[1:4, 1]
Error: subscript out of bounds
>
So I would have liked it to return:
[1] 1 2 3 NA
Right now I am doing this with ifelse tests to see if the index variables exist in the rownames but on large data structures this is taking quite a bit of time. here is an example:
s <- split(factors, factors$date) # split so each date has its own list
names <- last(s)[[1]]$bond # names of bonds that we want
cdmat <- sapply(names, function(n)
sapply(s, function(x)
if(n %in% x$bond) x[x$bond == n, column] else NA))
where factors is an xts with about 250 000 rows. So it's taking about 15 seconds and that's too long for my application.
The reason this is important is that each list element I am applying this to has a different length, but I need to output a matrix with equal length columns as a result of the sapply. I don't want another list out with different length elements.
Actually I have just realised that if I take the column I want and turn it into a vector, this works perfectly. So:
> b[, 1][1:4]
[1] 1 2 3 NA