Comparing matrices of different sizes in R

Asked

Viewed 517 times

1

I have the following situation :

all_sec = Matrix of all possible assets in a portfolio

all_sec <- matrix(c("SEC1","SEC2","SEC3","SEC4","SEC5"),ncol=1)

portfolio <- composition of a portfolio of assets (column 2 is equal to the weight of the asset in the portfolio

portfolio <- matrix(c("SEC2","SEC4",0.45,0.55),ncol=2)

I want as a result a 1 column Matrix in which the value returns 0 if the asset is not in the portfolio and returns the weight value (column 2 of the portfolio matrix) if it is in the portfolio. The result would be a Matrix in this format :

     [,1]  
[1,] 0
[2,] 0.45
[3,] 0
[4,] 0.55
[5,] 0

Could you help me ?

3 answers

2


You can do this with the merge:

resultado <- merge(all_sec, portfolio, all=TRUE) # faz o merge das duas matrizes
resultado <- apply(resultado, 2, as.character) # transforma em texto de novo (porque o default do R é transformar texto em factor
resultado[is.na(resultado)] <-0 #substitui NA por zero
dimnames(resultado) <- NULL # tira os nomes das dimensões

Then you will get the following result:

resultado
     [,1]   [,2]  
[1,] "SEC1" "0"   
[2,] "SEC2" "0.45"
[3,] "SEC3" "0"   
[4,] "SEC4" "0.55"
[5,] "SEC5" "0"   

And if you only want the second column:

subset(resultado, select=V2)
     [,1]  
[1,] "0"   
[2,] "0.45"
[3,] "0"   
[4,] "0.55"
[5,] "0"   

Now, one important thing to note is that its matrix portfolio is mixing elements character with numeric then she’ll turn everyone into character. Maybe you should work with a data.frame and not a matrix.

1

#define as matrizes
all_sec <- matrix(c("SEC1","SEC2","SEC3","SEC4","SEC5"),ncol=1)
portfolio <- matrix(c("SEC2","SEC4",0.45,0.55),ncol=2)
#define o tamanho das matrizes (melhor que ficar chamando dim() toda hora)
all_dim <- dim(all_sec)[1]
portfolio_dim <- dim(portfolio)[1]
#cria a matriz para guardar os resultados
resultado <- matrix(data=NA, nrow=all_dim, ncol=1) 
for(j in 1:all_dim){
   #inicia como 0
   resultado[j] <- 0
   #procura por X no portfolio
   for(i in 1:portfolio_dim){
      #se encontrar define o mesmo
      if(all_sec[j] == portfolio[i,1]){
        resultado[j] <- portfolio[i,2]
        break
      }
   }
}
print(resultado)

0

You can do this with the package data.table:

> library(data.table)

> all_sec <- matrix(c("SEC1","SEC2","SEC3","SEC4","SEC5"),ncol=1)
> portfolio <- matrix(c("SEC2","SEC4",0.45,0.55),ncol=2)
> all_sec = data.table(all_sec, key='V1')
> all_sec
     V1
1: SEC1
2: SEC2
3: SEC3
4: SEC4
5: SEC5
> portfolio = data.table(portfolio, key='V1')
> portfolio
     V1   V2
1: SEC2 0.45
2: SEC4 0.55
> portfolio[all_sec]
     V1   V2
1: SEC1   NA
2: SEC2 0.45
3: SEC3   NA
4: SEC4 0.55
5: SEC5   NA

> as.matrix(portfolio[all_sec][, list(V1, V2=as.numeric(as.character(V2)))][is.na(V2), V2 := 0][, V2])
     [,1]
[1,] 0.00
[2,] 0.45
[3,] 0.00
[4,] 0.55
[5,] 0.00
> 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.