Message Boards Message Boards

0
|
783 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Extracting sublists from the main list

Posted 2 months ago

Hi,

I have a list that looks like the following:

data = {{"westhumber", 0.037`}, {"westhumber", 0.034`}, {"westhumber",
0.037`}, {"westhumber", 0.076`}, {"westhumber", 
0.059`}, {"eldermills", 1.08`}, {"eldermills", 
1.01`}, {"eldermills", 1.22`}, {"coldcreek", 
0.818`}, {"coldcreek", 0.716`}, {"deasthumber", 
0.091`}, {"deasthumber", 0.255`}, {"deasthumber", 
0.235`}, {"deasthumber", 2.18`}, {"deasthumber", 1.7`}};

Now how to extract the sub-lists from the main list as the following :

list1 = {{"westhumber", 0.037`}, {"westhumber", 
   0.034`}, {"westhumber", 0.037`}, {"westhumber", 
   0.076`}, {"westhumber", 0.059`}}

list2 = {{"eldermills", 1.08`}, {"eldermills", 1.01`}, {"eldermills", 
   1.22`}}

list3 = {{"coldcreek", 0.818`}, {"coldcreek", 0.716`}}

list4 = {{"deasthumber", 0.255`}, {"deasthumber", 
   0.235`}, {"deasthumber", 2.18`}, {"deasthumber", 1.7`}}

Thank you in advance.

POSTED BY: Alex Teymouri
3 Replies
Posted 2 months ago

It may be more convenient to use this form depending on what you want to do next

grouped = GroupBy[data, First -> Last]

E.g.

{Mean@#, Median@#, StandardDeviation@#} & /@ grouped
POSTED BY: Rohit Namjoshi
Posted 2 months ago

GatherBy[data, First]

or

GroupBy[data, First]

(I think you're missing an element in your list4.)

POSTED BY: Eric Rimbey
Posted 2 months ago

Thanks, Eric.

That is a great solution way.

POSTED BY: Alex Teymouri
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract