[R] combining two different matrizes

Berend Hasselman bhh at xs4all.nl
Thu Jun 6 16:04:00 CEST 2013


On 05-06-2013, at 23:56, ThomasH <thomas.hufnagel1 at gmx.de> wrote:

> 
> Hello together,
> 
> this is ma first post, so please aplogize me if post this in the wrong
> section.
> 
> I have problem concerning ma two matrizes.
> 
> After a regressione and so on, I got two matrizes
> 
> Matrixres contains the results of ma calculation.
> 
> Matrixr contains my detiene, which where Aldo used for the regression.
> 
> Please ser the following code:
> 
> #Datei einlesen
> residual = read.csv2("E:***Input-R_Renditen.csv",header=TRUE, sep=";")
> 
> 
> #Aktientitel
> alist <- list()
> for (a in 2:11){
> 
> 
> #Regression
>    #Länge Gesamtzeit
>    t <- 243
>    tx <- t-59
> 
>    #Länge Regression
>    reglist <- list()
>    for (i in 1:tx){
>    j <- i+59
> 
>    #RegressionsVariable
>    x = residual[i:j,a]
>    rm = residual[i:j,12]
>    smb = residual[i:j,13]
>    hml = residual[i:j,14]
>    rf = residual[i:j,15]
> 
>    #Überschussrenditen
>    ex=x-rf
>    erm=rm-rf
> 
>    #Regression
>    reg <- lm(ex~erm+smb+hml)
>    reglist[[i]] <- coef(reg)
>    
> 
> #Berechnung Residuum
>       #Residual Berechnung
>       rx = residual[(j-5):j,a]
>       rrm = residual[(j-5):j,12]
>       rsmb = residual[(j-5):j,13]
>       rhml = residual[(j-5):j,14]
>       rrf = residual[(j-5):j,15]
> 
>       rex = rx-rrf
>       rerm = rrm-rrf
> 
>       #Berechnung
>       res <-
> sum(rex-(reglist[[i]][2]*rerm+reglist[[i]][3]*rsmb+reglist[[i]][4]*rhml))/sd(rex-(reglist[[i]][2]*rerm+reglist[[i]][3]*rsmb+reglist[[i]][4]*rhml))
>       reglist[[i]] <- res
> }   
>    
> 
> #Residuen auf alle Aktien
>    alist[[a]] <- reglist
> }
>    
> #Matrix mit Residuen
>    matrixres <- do.call(cbind,alist)
> 
> #Spaltennamen/Zeilennamen
> s<- names(residual)[2:11]
> colnames(matrixres)<-s
> 
> #RenditeMatrix
> matrixr <- do.call(cbind,residual[60:243,2:11])
> 
> 
> Now I want to combines  the two matrizes in the following way:
> 
> Under every row of matrixres should stand the row of matrixr for excample:
> 
> Matrixres row1
> Matrixr row1
> Matrixres row2
> Matrixr row 2
> 
> Can anybody help me? I was working on this problem the whole day, but have
> no idea.

Something like this (assuming A and B have the same dimensions)

set.seed(11)
A <- matrix(round(rnorm(25),3),nrow=5)
B <- matrix(1:25,nrow=5)

C <- matrix(0,nrow=2*nrow(A),ncol=ncol(A))

crows <- seq.int(from=1,to=2*nrow(A),by=2)
C[crows,] <- A
C[crows+1,] <- B

Some friendly advice: get someone to check your English before sending a mail to the list.

Berend



More information about the R-help mailing list