[R] Selecting and then joining data blocks
arun
smartpink111 at yahoo.com
Thu Apr 25 15:50:59 CEST 2013
In addition,
If your matrix names do not follow any particular pattern:
tiger<- matrix(1:20,ncol=5)
cat<- matrix(21:40,ncol=5)
dog<- matrix(41:60,ncol=5)
wolf<- matrix(61:80,ncol=5)
vec<- c(1,2,4,3,2,3,1)
vec2<- c("tiger","cat","dog","wolf")
#Suppose, you wanted the order to be "tiger", "cat", "dog", "wolf"
vec2<- factor(vec2,levels=vec2)
vec2
#[1] tiger cat dog wolf
#Levels: tiger cat dog wolf
res3<-do.call(rbind,lapply(vec,function(i) get(as.character(vec2[as.numeric(vec2)==i]))))
res3
# [,1] [,2] [,3] [,4] [,5]
#[1,] 1 5 9 13 17
#[2,] 2 6 10 14 18
#[3,] 3 7 11 15 19
#[4,] 4 8 12 16 20
#[5,] 21 25 29 33 37
#[6,] 22 26 30 34 38
#[7,] 23 27 31 35 39
#[8,] 24 28 32 36 40
#[9,] 61 65 69 73 77
#[10,] 62 66 70 74 78
#[11,] 63 67 71 75 79
#[12,] 64 68 72 76 80
#[13,] 41 45 49 53 57
#[14,] 42 46 50 54 58
#[15,] 43 47 51 55 59
#[16,] 44 48 52 56 60
#[17,] 21 25 29 33 37
#[18,] 22 26 30 34 38
#[19,] 23 27 31 35 39
#[20,] 24 28 32 36 40
#[21,] 41 45 49 53 57
#[22,] 42 46 50 54 58
#[23,] 43 47 51 55 59
#[24,] 44 48 52 56 60
#[25,] 1 5 9 13 17
#[26,] 2 6 10 14 18
#[27,] 3 7 11 15 19
#[28,] 4 8 12 16 20
A.K.
----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: Preetam Pal <lordpreetam at gmail.com>
Cc:
Sent: Thursday, April 25, 2013 9:03 AM
Subject: Re: [R] Selecting and then joining data blocks
HI Preetam,
I created the matrices in a list because it was easier to create. If you look at the second solution:
B1<- lst1[[1]]
B2<- lst1[[2]]
B3<- lst1[[3]]
B4<- lst1[[4]]
Consider that B1, B2, B3, B4 are your actual matrices and apply the solution below:
paste0("B",vec) #gives the names of the matrices
#[1] "B1" "B2" "B4" "B3" "B2" "B3" "B1"
using get(), will get the matrices stored in that names.
res2<-do.call(rbind,lapply(vec,function(i) get(paste0("B",i))))
If the names of the matrices are different, you need to change it accordingly. I programmed it based on the information you gave.
I hope this helps.
Arun
________________________________
From: Preetam Pal <lordpreetam at gmail.com>
To: arun <smartpink111 at yahoo.com>
Sent: Thursday, April 25, 2013 8:53 AM
Subject: Re: [R] Selecting and then joining data blocks
Hi Arun,
Thanks for your solution. But there is only 1 thing which i could not understand:
In my case, the 4 matirces(B1,B2,B3,B4) were already specified and i have to work with these only...how do I accommodate that (instead of letting R produce the big matrix by random sampling)..This might be very trivial, but i am a starter with R...I shall really appreciate if you could advise me on this.
Again thanks,
Preetam
On Thu, Apr 25, 2013 at 5:44 PM, arun <smartpink111 at yahoo.com> wrote:
HI,
>set.seed(24)
>#creating the four matrix in a list
>
>lst1<-lapply(1:4,function(x) matrix(sample(1:40,20,replace=TRUE),ncol=5))
>names(lst1)<- paste0("B",1:4)
>vec<- c(1,2,4,3,2,3,1)
>res<-do.call(rbind,lapply(vec,function(i) lst1[[i]]))
>dim(res)
>#[1] 28 5
>
>
>#or
>B1<- lst1[[1]]
> B2<- lst1[[2]]
> B3<- lst1[[3]]
> B4<- lst1[[4]]
>
> res2<-do.call(rbind,lapply(vec,function(i) get(paste0("B",i))))
> identical(res,res2)
>#[1] TRUE
>A.K.
>
>
>
>
>
>----- Original Message -----
>From: Preetam Pal <lordpreetam at gmail.com>
>To: r-help at r-project.org
>Cc:
>Sent: Thursday, April 25, 2013 7:51 AM
>Subject: [R] Selecting and then joining data blocks
>
>Hi all,
>
>I have 4 matrices, each having 5 columns and 4 rows .....denoted by
>B1,B2,B3,B4.
>I have generated a vector of 7 indices, say (1,2,4,3,2,3,1} which refers to
>the index of the matrices to be chosen and then appended one on the top of
>the next: like, in this case, I wish to have the following mega matrix:
>B1over B2 over B4 over B3 over B2 over B3 over B1.
>
>1> How can I achieve this?
>2> I don't want to manually identify and arrange the matrices for each
>vector of index values generated (for which the code I used is :
>index=sample( 4,7,replace=T)). How can I automate the process?
>
>Basically, I am doing bootstrapping , but the observations are actually 4X5
>matrices.
>
>Appreciate your help.
>
>
>Thanks,
>Preetam
>
>
>---
>
>Preetam Pal
>(+91)-9432212774
>M-Stat 2nd Year, Room No. N-114
>Statistics Division, C.V.Raman
>Hall
>Indian Statistical Institute, B.H.O.S.
>Kolkata.
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>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.
>
>
--
Preetam Pal (+91)-9432212774
M-Stat 2nd Year, Room No. N-114
Statistics Division, C.V.Raman Hall
Indian Statistical Institute, B.H.O.S.
Kolkata.
More information about the R-help
mailing list