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
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
> -Original Message-
> 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]
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%
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 <- s
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 bc
[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
6 matches
Mail list logo