Re: List of position-specifying functions
- To: mathgroup at smc.vnet.net
- Subject: [mg102410] Re: [mg102351] List of position-specifying functions
- From: Ray Koopman <koopman at sfu.ca>
- Date: Sat, 8 Aug 2009 04:39:06 -0400 (EDT)
Suppose I have a data matrix in which row are cases and columns are variables, with the variables names in a list: varnames = {"id","sex","age","eth","dx1","dx2",<<others>>}; If I want to group the cases by the values of some of the variables -- say sex, ethnicity, and primary diagnosis -- I might do: vlist = {"sex","eth","dx1"}; plist = Flatten[Position[varnames,#]&/@vlist]; GatherBy[data,Function[p,#[[p]]&]/@plist] ----- <DrMajorBob at bigfoot.com> wrote: > Here's the same thing with Map: > > plist = {2, 5, 4, 6, 3}; > Function[x, #[[x]] &] /@ plist > > {#1[[2]] &, #1[[5]] &, #1[[4]] &, #1[[6]] &, #1[[3]] &} > > I'm mystified by how you'd use this with SplitBy or GatherBy, though. > > Send an example, if you can! > > Bobby > > On Thu, 06 Aug 2009 05:32:16 -0500, Ray Koopman <koopman at sfu.ca> wrote: > >> I have a list of distinct positive integers, such as >> >> In[1]:= plist = {2, 5, 4, 6, 3}; >> >> I want to convert it to a list of position-specifying functions, >> such as might be used as the second argument in SplitBy or GatherBy. >> One way that works is: >> >> In[2]:= plist /. p_Integer->(#[[p]]&) >> >> Out[2]= { #1[[2]]&, #1[[5]]&, #1[[4]]&, #1[[6]]&, #1[[3]]& } >> >> I've been trying to get the same thing using Map, >> but I haven't been able to do it. Any suggestions? > > -- > DrMajorBob at bigfoot.com
- Follow-Ups:
- Re: Re: List of position-specifying functions
- From: DrMajorBob <btreat1@austin.rr.com>
- Re: Re: List of position-specifying functions