[R] create a function with "subset" statement

Jim Lemon drjimlemon at gmail.com
Thu Jan 29 22:43:09 CET 2015


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:
>
>
>> -----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 u...{{dropped:8}}



More information about the R-help mailing list