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
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]]
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 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]]
Or something like
lapply(subsets, function(sub, x) do.call(subset,list(x,subset=eval(parse(text=sublist[[sub]])))), x=x)
?
*******************************************************************
This email and any attachments are confidential. Any use, copying or
disclosure other than by the intended recipient is unauthorised. If
you have received this message in error, please notify the sender
immediately via +44(0)20 8943 7000 or notify postmaster at lgcgroup.com
and delete this message and any copies from your computer and network.
LGC Limited. Registered in England 2991879.
Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK
Hi S,
I did try (briefly) to work out a way to use lapply but couldn't quite
get it right. After a bit of fooling around the following does work as
a standalone command:
lapply(subsets, function(sub, x)
do.call(subset,list(x,subset=eval(parse(text=sub)))), x=A)
Thanks for the improvement.
Jim
On Thu, Jan 29, 2015 at 10:32 PM, S Ellison <S.Ellison at lgcgroup.com> wrote:
Or something like
lapply(subsets, function(sub, x) do.call(subset,list(x,subset=eval(parse(text=sublist[[sub]])))), x=x)
?
*******************************************************************
This email and any attachments are confidential. Any u...{{dropped:8}}
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.
function(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
}