Re: [R] create a function with "subset" statement

2015-01-29 Thread JSHuang
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

Re: [R] create a function with "subset" statement

2015-01-29 Thread Jim Lemon
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

Re: [R] create a function with "subset" statement

2015-01-29 Thread S Ellison
> -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]

Re: [R] create a function with "subset" statement

2015-01-28 Thread Jim Lemon
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%

Re: [R] create a function with "subset" statement

2015-01-28 Thread Karim Mezhoud
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

[R] create a function with "subset" statement

2015-01-28 Thread Kathryn Lord
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