Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

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: [mg102522] Re: [mg102410] Re: [mg102351] List of position-specifying functions
  • From: Ray Koopman <koopman at sfu.ca>
  • Date: Wed, 12 Aug 2009 04:34:45 -0400 (EDT)

My raw data matrix would be the transpose of what you used. Each row
would be a case, each column would be a variable, and each element
would be the value of a particular case on a particular variable.
My intention was to use GatherBy to group the cases on the basis
of a subset of the variables. The output would be a k-way table in
which each the content of each cell was a cases-by-variables matrix.
So yes, the output would be messy. but it would be intended more for
statistical analyses than for visual inspection.

However, I forgot that GatherBy groups without sorting, so the output
table would be difficult to use because its cells would be in a
scrambled order.

----- DrMajorBob <btreat1 at austin.rr.com> wrote:
> 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: Dynamically create array and display data graphically
  • Next by Date: Formatting MatrixPower in TraditionalForm
  • Previous by thread: Re: Re: List of position-specifying functions
  • Next by thread: Linear Interpolation problems