[R] create a function with "subset" statement

S Ellison S.Ellison at LGCGroup.com
Thu Jan 29 12:32:22 CET 2015

> -----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] %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)
> }

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

More information about the R-help mailing list