[R] combining two different matrizes

arun smartpink111 at yahoo.com
Thu Jun 6 17:29:22 CEST 2013


Perhaps this also helps:
library(plyr)

 do.call(rbind,alply(aperm(laply(list(A,B),as.matrix),c(1,3,2)),3)) #Using Berend's example
           1      2      3      4      5
# [1,] -0.591 -0.934 -0.828  0.012 -0.683
 #[2,]  1.000  6.000 11.000 16.000 21.000
 #[3,]  0.027  1.324 -0.348 -0.223 -0.016
 #[4,]  2.000  7.000 12.000 17.000 22.000
 #[5,] -1.517  0.625 -1.538  0.888 -0.443
 #[6,]  3.000  8.000 13.000 18.000 23.000
 #[7,] -1.363 -0.046 -0.256 -0.592  0.353
 #[8,]  4.000  9.000 14.000 19.000 24.000
 #[9,]  1.178 -1.004 -1.150 -0.656  0.073
#[10,]  5.000 10.000 15.000 20.000 25.000


A.K.

----- Original Message -----
From: Berend Hasselman <bhh at xs4all.nl>
To: ThomasH <thomas.hufnagel1 at gmx.de>
Cc: r-help at r-project.org
Sent: Thursday, June 6, 2013 10:04 AM
Subject: Re: [R] combining two different matrizes


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

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list