MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: List of position-specifying functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg102476] Re: [mg102410] Re: [mg102351] List of position-specifying functions
  • From: DrMajorBob <btreat1 at austin.rr.com>
  • Date: Mon, 10 Aug 2009 04:17:19 -0400 (EDT)
  • References: <200908080839.EAA01309@smc.vnet.net>
  • Reply-to: drmajorbob at bigfoot.com

You didn't specify "data", so I'm still unsure what you're trying to do.

Here's a guess, in which the end result is awfully messy:

varnames = {"id", "sex", "age", "eth", "dx1", "dx2", "other1",
    "other2"};
data = RandomInteger[{0, 2}, {Length@varnames, 20}];
vlist = {"sex", "eth", "dx1"};
plist = Flatten[Position[varnames, #] & /@ vlist];
GatherBy[data, Function[p, #[[p]] &] /@ plist]

{{{{{2, 0, 1, 1, 0, 2, 2, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1,
      2}}}, {{{0, 0, 0, 0, 2, 0, 2, 1, 2, 2, 0, 0, 2, 2, 1, 2, 1, 0, 2,
       0}}}}, {{{{1, 1, 0, 0, 1, 2, 0, 0, 2, 0, 0, 0, 2, 2, 2, 0, 0, 1,
       1, 1}}}, {{{1, 1, 2, 1, 1, 2, 2, 1, 2, 0, 1, 2, 2, 1, 2, 2, 0,
      0, 2, 2}}, {{0, 1, 2, 1, 2, 2, 1, 1, 2, 0, 1, 2, 0, 2, 1, 2, 2,
      0, 2, 0}}}, {{{0, 1, 2, 2, 2, 1, 2, 1, 1, 0, 0, 1, 0, 2, 0, 0, 0,
       2, 0, 0}}}}, {{{{0, 2, 1, 2, 0, 0, 2, 0, 2, 0, 1, 2, 0, 2, 1, 0,
       2, 0, 1, 1}}}, {{{2, 2, 0, 1, 0, 0, 0, 2, 0, 1, 0, 1, 0, 1, 0,
      1, 2, 2, 0, 1}}}}}

You've really found a use for that? Anything is possible, I guess.

Bobby

On Sat, 08 Aug 2009 03:39:06 -0500, Ray Koopman <koopman at sfu.ca> wrote:

> 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
>



-- 
DrMajorBob at bigfoot.com


  • Prev by Date: Re: Re: Re: A Sum-like notation for iteration
  • Next by Date: Re: Re: Re: error with Sum and Infinity
  • Previous by thread: Re: List of position-specifying functions
  • Next by thread: Re: Re: List of position-specifying functions