Dear R experts, Suppose I have a matrix A below. a <- rep(1:4, each=5) b <- rep(1:5, 4) c <- rnorm(20) A <- cbind(a,b,c)> Aa b c [1,] 1 1 0.761806718 [2,] 1 2 0.239734573 [3,] 1 3 -0.728339238 [4,] 1 4 -0.121946174 [5,] 1 5 -0.131909077 [6,] 2 1 -0.069790098 [7,] 2 2 1.082671767 [8,] 2 3 -0.869537195 [9,] 2 4 -0.417222758 [10,] 2 5 -2.432273481 [11,] 3 1 0.425432121 [12,] 3 2 -2.453299938 [13,] 3 3 0.612125174 [14,] 3 4 -0.005387462 [15,] 3 5 1.911146222 [16,] 4 1 0.161408685 [17,] 4 2 0.567118882 [18,] 4 3 -0.948882839 [19,] 4 4 0.485002340 [20,] 4 5 -0.551981333 With this matrix A, I'd like to create several sub-matrices, for example B <- subset(A, (A[,1] %in% c(1,2) & A[,2] %in% c(1,2)) | (A[,1] %in% c(3) & A[,2] %in% c(1) ) | (A[,1] %in% c(4) & A[,2] %in% c(1:4)) )> Ba b c [1,] 1 1 0.7618067 [2,] 1 2 0.2397346 [3,] 2 1 -0.0697901 [4,] 2 2 1.0826718 [5,] 3 1 0.4254321 [6,] 4 1 0.1614087 [7,] 4 2 0.5671189 [8,] 4 3 -0.9488828 [9,] 4 4 0.4850023 or C <- subset(A, (A[,1] %in% c(1:4) & A[,2] %in% c(1,2)) )> Ca b c [1,] 1 1 0.7618067 [2,] 1 2 0.2397346 [3,] 2 1 -0.0697901 [4,] 2 2 1.0826718 [5,] 3 1 0.4254321 [6,] 3 2 -2.4532999 [7,] 4 1 0.1614087 [8,] 4 2 0.5671189 or D <- subset(A, (A[,1] %in% c(1,2) & A[,2] %in% c(1:3)) | (A[,1] %in% c(3) & A[,2] %in% c(1,2)) )> Da b c [1,] 1 1 0.7618067 [2,] 1 2 0.2397346 [3,] 1 3 -0.7283392 [4,] 2 1 -0.0697901 [5,] 2 2 1.0826718 [6,] 2 3 -0.8695372 [7,] 3 1 0.4254321 [8,] 3 2 -2.4532999 and so forth. I am wondering if I could create matrices B, C, D etc AT ONE TIME. In order to do that, I guess I need to make a "function". unfortunately, I have no idea how to do that. Any suggestion will be greatly appreciated. Kathryn Lord [[alternative HTML version deleted]]
Hi,
You did the harder, it remains the easier
listMatrices <- vector("list", 3)
doAll <- function(A){
B <- subset(A, (A[,1] %in% c(1,2) & A[,2] %in% c(1,2)) |
(A[,1] %in% c(3) & A[,2] %in% c(1) ) |
(A[,1] %in% c(4) & A[,2] %in% c(1:4)) )
C <- subset(A, (A[,1] %in% c(1:4) & A[,2] %in% c(1,2)) )
D <- subset(A, (A[,1] %in% c(1,2) & A[,2] %in% c(1:3)) |
(A[,1] %in% c(3) & A[,2] %in% c(1,2)) )
return(B)
}
B<- doAll(A)
verify if you can:
return(c(B,C,D)).
listMatrices <- doAll(A)
Karim
?__
c/ /'_;~~~~kmezhoud
(*) \(*) ????? ??????
http://bioinformatics.tn/
On Wed, Jan 28, 2015 at 8:54 PM, Kathryn Lord <kathryn.lord2000 at
gmail.com>
wrote:
> Dear R experts,
>
> Suppose I have a matrix A below.
>
> a <- rep(1:4, each=5)
> b <- rep(1:5, 4)
> c <- rnorm(20)
>
> A <- cbind(a,b,c)
>
> > A
> a b c
> [1,] 1 1 0.761806718
> [2,] 1 2 0.239734573
> [3,] 1 3 -0.728339238
> [4,] 1 4 -0.121946174
> [5,] 1 5 -0.131909077
> [6,] 2 1 -0.069790098
> [7,] 2 2 1.082671767
> [8,] 2 3 -0.869537195
> [9,] 2 4 -0.417222758
> [10,] 2 5 -2.432273481
> [11,] 3 1 0.425432121
> [12,] 3 2 -2.453299938
> [13,] 3 3 0.612125174
> [14,] 3 4 -0.005387462
> [15,] 3 5 1.911146222
> [16,] 4 1 0.161408685
> [17,] 4 2 0.567118882
> [18,] 4 3 -0.948882839
> [19,] 4 4 0.485002340
> [20,] 4 5 -0.551981333
>
>
> With this matrix A, I'd like to create several sub-matrices, for
example
>
>
> B <- subset(A, (A[,1] %in% c(1,2) & A[,2] %in% c(1,2)) |
> (A[,1] %in% c(3) & A[,2] %in% c(1) ) |
> (A[,1] %in% c(4) & A[,2] %in% c(1:4)) )
>
> > B
> a b c
> [1,] 1 1 0.7618067
> [2,] 1 2 0.2397346
> [3,] 2 1 -0.0697901
> [4,] 2 2 1.0826718
> [5,] 3 1 0.4254321
> [6,] 4 1 0.1614087
> [7,] 4 2 0.5671189
> [8,] 4 3 -0.9488828
> [9,] 4 4 0.4850023
>
>
> or
>
> C <- subset(A, (A[,1] %in% c(1:4) & A[,2] %in% c(1,2)) )
>
> > C
> a b c
> [1,] 1 1 0.7618067
> [2,] 1 2 0.2397346
> [3,] 2 1 -0.0697901
> [4,] 2 2 1.0826718
> [5,] 3 1 0.4254321
> [6,] 3 2 -2.4532999
> [7,] 4 1 0.1614087
> [8,] 4 2 0.5671189
>
>
> or
>
> D <- subset(A, (A[,1] %in% c(1,2) & A[,2] %in% c(1:3)) |
> (A[,1] %in% c(3) & A[,2] %in% c(1,2)) )
>
> > D
> a b c
> [1,] 1 1 0.7618067
> [2,] 1 2 0.2397346
> [3,] 1 3 -0.7283392
> [4,] 2 1 -0.0697901
> [5,] 2 2 1.0826718
> [6,] 2 3 -0.8695372
> [7,] 3 1 0.4254321
> [8,] 3 2 -2.4532999
>
> and so forth.
>
> I am wondering if I could create matrices B, C, D etc AT ONE TIME. In order
> to do that, I guess I need to make a "function". unfortunately, I
have no
> idea how to do that.
>
> Any suggestion will be greatly appreciated.
>
> Kathryn Lord
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>
[[alternative HTML version deleted]]
Hi Kathryn,
If you construct a list of your logical conditions, you can pass that
to a function that evaluates them one by one and returns a list of the
resulting subsets.
subsets<-list(B="(A[,1] %in% c(1,2) & A[,2] %in% c(1,2)) | (A[,1]
%in%
c(3) & A[,2] %in% c(1)) | (A[,1] %in% c(4) & A[,2] %in% c(1:4))",
C="(A[,1] %in% c(1:4) & A[,2] %in% c(1,2))",
D="(A[,1] %in% c(1,2) & A[,2] %in% c(1:3)) | (A[,1] %in% c(3) &
A[,2]
%in% c(1,2))")
multi_subset<-function(x,sublist) {
result_list<-list()
for(sub in 1:length(sublist))
result_list[[sub]]<-do.call(subset,list(x,subset=eval(parse(text=sublist[[sub]]))))
names(result_list)<-names(sublist)
return(result_list)
}
Jim
On Thu, Jan 29, 2015 at 7:43 AM, Karim Mezhoud <kmezhoud at gmail.com>
wrote:> Hi,
> You did the harder, it remains the easier
>
> listMatrices <- vector("list", 3)
>
> doAll <- function(A){
> B <- subset(A, (A[,1] %in% c(1,2) & A[,2] %in% c(1,2)) |
> (A[,1] %in% c(3) & A[,2] %in% c(1) ) |
> (A[,1] %in% c(4) & A[,2] %in% c(1:4)) )
>
> C <- subset(A, (A[,1] %in% c(1:4) & A[,2] %in% c(1,2)) )
>
> D <- subset(A, (A[,1] %in% c(1,2) & A[,2] %in% c(1:3)) |
> (A[,1] %in% c(3) & A[,2] %in% c(1,2)) )
>
> return(B)
> }
>
> B<- doAll(A)
>
>
> verify if you can:
>
> return(c(B,C,D)).
>
> listMatrices <- doAll(A)
>
> Karim
>
> ?__
> c/ /'_;~~~~kmezhoud
> (*) \(*) ????? ??????
> http://bioinformatics.tn/
>
>
>
> On Wed, Jan 28, 2015 at 8:54 PM, Kathryn Lord <kathryn.lord2000 at
gmail.com>
> wrote:
>
>> Dear R experts,
>>
>> Suppose I have a matrix A below.
>>
>> a <- rep(1:4, each=5)
>> b <- rep(1:5, 4)
>> c <- rnorm(20)
>>
>> A <- cbind(a,b,c)
>>
>> > A
>> a b c
>> [1,] 1 1 0.761806718
>> [2,] 1 2 0.239734573
>> [3,] 1 3 -0.728339238
>> [4,] 1 4 -0.121946174
>> [5,] 1 5 -0.131909077
>> [6,] 2 1 -0.069790098
>> [7,] 2 2 1.082671767
>> [8,] 2 3 -0.869537195
>> [9,] 2 4 -0.417222758
>> [10,] 2 5 -2.432273481
>> [11,] 3 1 0.425432121
>> [12,] 3 2 -2.453299938
>> [13,] 3 3 0.612125174
>> [14,] 3 4 -0.005387462
>> [15,] 3 5 1.911146222
>> [16,] 4 1 0.161408685
>> [17,] 4 2 0.567118882
>> [18,] 4 3 -0.948882839
>> [19,] 4 4 0.485002340
>> [20,] 4 5 -0.551981333
>>
>>
>> With this matrix A, I'd like to create several sub-matrices, for
example
>>
>>
>> B <- subset(A, (A[,1] %in% c(1,2) & A[,2] %in% c(1,2)) |
>> (A[,1] %in% c(3) & A[,2] %in% c(1) ) |
>> (A[,1] %in% c(4) & A[,2] %in% c(1:4)) )
>>
>> > B
>> a b c
>> [1,] 1 1 0.7618067
>> [2,] 1 2 0.2397346
>> [3,] 2 1 -0.0697901
>> [4,] 2 2 1.0826718
>> [5,] 3 1 0.4254321
>> [6,] 4 1 0.1614087
>> [7,] 4 2 0.5671189
>> [8,] 4 3 -0.9488828
>> [9,] 4 4 0.4850023
>>
>>
>> or
>>
>> C <- subset(A, (A[,1] %in% c(1:4) & A[,2] %in% c(1,2)) )
>>
>> > C
>> a b c
>> [1,] 1 1 0.7618067
>> [2,] 1 2 0.2397346
>> [3,] 2 1 -0.0697901
>> [4,] 2 2 1.0826718
>> [5,] 3 1 0.4254321
>> [6,] 3 2 -2.4532999
>> [7,] 4 1 0.1614087
>> [8,] 4 2 0.5671189
>>
>>
>> or
>>
>> D <- subset(A, (A[,1] %in% c(1,2) & A[,2] %in% c(1:3)) |
>> (A[,1] %in% c(3) & A[,2] %in% c(1,2)) )
>>
>> > D
>> a b c
>> [1,] 1 1 0.7618067
>> [2,] 1 2 0.2397346
>> [3,] 1 3 -0.7283392
>> [4,] 2 1 -0.0697901
>> [5,] 2 2 1.0826718
>> [6,] 2 3 -0.8695372
>> [7,] 3 1 0.4254321
>> [8,] 3 2 -2.4532999
>>
>> and so forth.
>>
>> I am wondering if I could create matrices B, C, D etc AT ONE TIME. In
order
>> to do that, I guess I need to make a "function".
unfortunately, I have no
>> idea how to do that.
>>
>> Any suggestion will be greatly appreciated.
>>
>> Kathryn Lord
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
Hi, The following is implemented with function named subsetAll. It calls the function subset1. The input to subset1 is the matrix A and a vector indicating the beginning and ending values for columns 1 and 2. Input to subsetAll is the matrix A and a matrix that is row bind from those vectors constitute the conditions.> subset1function(A,v) { A[A[,1] %in% v[1]:v[2] & A[,2] %in% v[3]:v[4],] }> subsetAllfunction(A,m) { v <- as.vector(m[1,]) result <- subset1(A,v) if (dim(m)[1] > 1) { for (i in 2:dim(m)[1]) { v <- as.vector(m[i,]) result <- rbind(result,subset1(A,v)) } } result }> Aa b c [1,] 1 1 -0.04160857 [2,] 1 2 0.75486670 [3,] 1 3 -0.21022257 [4,] 1 4 -1.02594314 [5,] 1 5 -1.91864901 [6,] 2 1 0.19760459 [7,] 2 2 -1.30046908 [8,] 2 3 0.30554275 [9,] 2 4 -0.41861494 [10,] 2 5 0.22763712 [11,] 3 1 1.20244853 [12,] 3 2 1.15425304 [13,] 3 3 -2.13727010 [14,] 3 4 -0.27288205 [15,] 3 5 -1.08974847 [16,] 4 1 0.44501360 [17,] 4 2 -2.79163479 [18,] 4 3 -0.26713804 [19,] 4 4 1.31392292 [20,] 4 5 1.75383175> (mB <- rbind(c(1,2,1,2),c(3,3,1,1),c(4,4,1,4)))[,1] [,2] [,3] [,4] [1,] 1 2 1 2 [2,] 3 3 1 1 [3,] 4 4 1 4> (B <- subsetAll(A,mB))a b c [1,] 1 1 -0.04160857 [2,] 1 2 0.75486670 [3,] 2 1 0.19760459 [4,] 2 2 -1.30046908 [5,] 3 1 1.20244853 [6,] 4 1 0.44501360 [7,] 4 2 -2.79163479 [8,] 4 3 -0.26713804 [9,] 4 4 1.31392292> (mC <- rbind(c(1,4,1,2)))[,1] [,2] [,3] [,4] [1,] 1 4 1 2> (C <- subsetAll(A,mC))a b c [1,] 1 1 -0.04160857 [2,] 1 2 0.75486670 [3,] 2 1 0.19760459 [4,] 2 2 -1.30046908 [5,] 3 1 1.20244853 [6,] 3 2 1.15425304 [7,] 4 1 0.44501360 [8,] 4 2 -2.79163479> (mD <- rbind(c(1,2,1,3),c(3,3,1,2)))[,1] [,2] [,3] [,4] [1,] 1 2 1 3 [2,] 3 3 1 2> (D <- subsetAll(A,mD))a b c [1,] 1 1 -0.04160857 [2,] 1 2 0.75486670 [3,] 1 3 -0.21022257 [4,] 2 1 0.19760459 [5,] 2 2 -1.30046908 [6,] 2 3 0.30554275 [7,] 3 1 1.20244853 [8,] 3 2 1.15425304>-- View this message in context: http://r.789695.n4.nabble.com/create-a-function-with-subset-statement-tp4702423p4702497.html Sent from the R help mailing list archive at Nabble.com.