Group Abstract Group Abstract

Message Boards Message Boards

0
|
17.5K Views
|
21 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How can i select a subset of information with mathematica 9?

Hello every one, i am learning how to use mathematica, i am using mathematica 9. My problem is that when i have several groups in a column i have to create several spreadsheets in order to separate the groups and be able to make operation between them.

I would like to know how to select the sub groups without creating several spreadsheets.

I add a spreadsheets and a mathematica notebook with some data in which i working on

Thank you very much.

Attachments:
POSTED BY: Oscar Rodriguez
21 Replies

Thanks Otto,

I tried the code, but for some reason is not working, i don't know if i doing something wrong.

Thank you very much

Attachments:
POSTED BY: Oscar Rodriguez

Oscar,

You probably want:

Part[Select[data, And[#[[1]] == "1 VTS",#[[7]]=="Female"] &], All, 3]

This will only give you the values in column 3 for the entries that correspond to 1 VST and females. If you also want to see the word "Female" in those entries, then you would do:

Part[Select[data, And[#[[1]] == "1 VTS",#[[7]]=="Female"] &], All, {3,7}]

You could also extract the columns 3 and 7 for both males and females (for 1 VTS) and then segregate males and females into two sublists. That would be:

GatherBy[Part[Select[data, #[[1]] == "1 VTS" &], All, {3,7}],#[[2]]&]

If after this segregation you no longer want to keep the values "Male" and "Female", then you could do:

Drop[GatherBy[Part[Select[data, #[[1]] == "1 VTS" &], All, {3,7}],#[[2]]&],None,None,2]

This is what I meant when I said you could use this to plot both datasets in the same graph (with different plot markers). The output of the command above should be a list consisting of two sublists (probably of different lengths), and each one of those is just a list of numbers. If you do a ListPlot of the output, then you should get such a plot. The x-axis will just be a counter.

Hope it helps,

OL.

POSTED BY: Otto Linsuain

Hello Otto,

I was about to say that it is great to have the option to have help in spanish...

So i try this

In[49] Part[Select[data, #[[1]] == "1 VTS" &], All, 3](Selecciona la columna 3 asociada a 1 VTS)

Out[49] {2., 1., 1., 3., 2., 3., 2., 5., 4.}

This allowed me to get the data of the column 3 associated to 1 VST. Then i tried to get the data of the column 3 associated to the sex of the participants

In[30] Part[Select[data, #[[1]] == "1 VTS" &], All, {3, 7}]

Out[30] {{2., "Female "}, {1., "Male "}, {1., "Female "}, {3., "Female "}, {2., "Female "}, {3., "Male "}, {2., "Female "}, {5., "Male "}, {4., "Female "}}

But i am no able to get the data of the column 3 associated to a specific sex, for example the data associated only to females. Could you indicate me how to do that?

POSTED BY: Oscar Rodriguez
POSTED BY: Otto Linsuain

This forum supports currently only English language. Please use only English in your discussions so moderators and other members can understand the content.

POSTED BY: EDITORIAL BOARD

Oscar y Luis,

Me parece que lo que Oscar quiere se debe poder hacer así:

data = Import["/home/bird/Documents/ejemplo1.csv", "CSV"];
Part[Select[data, #[[1]] == "1 VTS" &], All, 3]

{2, 1, 1, 3, 2, 3, 2, 5, 4}

Yo también hago bastante trabajo en Excel, pero para listas grandes se traba mucho y la funcionalidad es mucho más limitada (me parece a mí).

Yo hago mucho trabajo que incluye arreglos bastante grades usando sólo las funciones normales de listas (Select, Map, MapThread, Thread, MapAt, MapIndexed, Apply, Part, Take, Drop, Join, Insert, Append, Prepend, Flatten, Partition, etc.). Todas estas funciones resuelven la mayoría de los problemas de manipular listas (o listas de listas de listas).

Mathematica tiene además una maquinaria un poco más sofisticada (que yo no he usado mucho) para manipular bases de datos. Si trabajas mucho con bases de datos busca la función Dataset en la documentación. Pero yo creo que tiene sentido primero familiarizarse con las estructuras básicas, como las que menciono arriba.

Saludos,

OL.

POSTED BY: Otto Linsuain

Hola Otto,

Muchas gracias por ayudarme a entender aun más el funcionamiento de estas herramientas, todos son muy amables. Ese comando que me muestras me permite seleccionar lo que necesito, es decir, solo la columna 3 asociada a 1 VTS, me gustaría saber si con ese mimo comando se puede hacer algo como seleccionar la columna 3 pero esta vez asociada al sexo de los participantes, intenté esto pero no funciona Part[Select[data, #[[1]] == "1 VTS" &], All, 3]

Muchas gracias

POSTED BY: Oscar Rodriguez
Posted 10 years ago

No entendi bien tu pregunta, pero si lo que intentas hacer es solamente tomar los elementos de la tercera columna de la variable data sin "1 VTS" podrias usar la misma idea que te comparti en mi comentario anterior. Otra posiblilidad es que quieras una vez seleccionados los pares {"1 VTS",elemento columna3} solamente tomar todos los elementos de la columna 3 podrias hacerlo asi

Cases[data, {"1 VTS", _, _, _, _, _, _}][[;; , 
    1 ;; 3 ;; 2]][[;; , 2]]

o si necesitas tener los pares {"1 VTs",elemento columna3} almacenados y despues utilizar la columna 3 creo que podrias utilizar variables, para asi tener disponibles los datos que necesitas

hu = Cases[data, {"1 VTS", _, _, _, _, _, _}][[;; , 
    1 ;; 3 ;; 2]][[;; , 2]]

In[9]:= hu[[;; , 2]]

Out[9]= {2., 1., 1., 3., 2., 3., 2., 5., 4.}

Espero haberte ayudado en algo, pero si sigues teniendo algunos problemas por fa hazmelos saber tal vez entre los dos tengamos alguna solucion.

POSTED BY: Luis Ledesma

Hola Luis,

eres muy amable, ya he logrado individualizar vectores y hacer operaciones entre ellos (como se ve en la imagen), lo cual era mi objetivo, aún no domino muy bien la técnica pero creo que es cuestión de practicar. Muchas gracias por la guía que me has dado

Ssaludos enter image description here

POSTED BY: Oscar Rodriguez
Posted 10 years ago
POSTED BY: Luis Ledesma
POSTED BY: Oscar Rodriguez
POSTED BY: Otto Linsuain
Attachments:
POSTED BY: Oscar Rodriguez

thank you very much, now i will work more fluently with mathematica

POSTED BY: Oscar Rodriguez
Posted 10 years ago

Hi Oscar,

Attached is a notebook example which imports your data. It then uses the Mathematca function GatherBy to group the data into sublists, where each sublist contains the records for a given protocol. At this point, a separate but identical analysis can be run on each of these sublists to get a separate analysis for each protocol. I would usually do that be making a function that performs an analysis and then mapping it onto the data collection, but that is a bit advanced. You can also copy the analysis, with new input. Where each input is one of the sublists.

Best, David

Attachments:
POSTED BY: David Keith

i really apreciety your help. I hope this new file be more simple

Attachments:
POSTED BY: Oscar Rodriguez
Posted 10 years ago
POSTED BY: David Keith

i know that it is probably an annoyance, i am learning mathematica and I can not follow you, could you please make the example directly in the mathematica notebook with my data? I will appreciate

POSTED BY: Oscar Rodriguez
Posted 10 years ago

I checked -- it was not new syntax in 10. I think it's been available for quite a while.

You can use select together with a selection condition to extract rows by content. For example, code below uses a pure function that returns True for a particular match. A row is selected if the 2nd column contains precisely "B"

In[6]:= set = {"A", "B", "C", "D"};

In[7]:= data = RandomChoice[set, {9, 5}]

Out[7]= {{"D", "D", "B", "C", "A"}, {"A", "C", "A", "A", "C"}, {"C", 
  "B", "D", "B", "C"}, {"B", "A", "A", "C", "C"}, {"B", "B", "A", "B",
   "C"}, {"B", "C", "B", "B", "C"}, {"D", "D", "B", "D", "A"}, {"D", 
  "C", "D", "C", "D"}, {"A", "A", "C", "B", "A"}}

In[8]:= Select[data, #[[2]] === "B" &]

Out[8]= {{"C", "B", "D", "B", "C"}, {"B", "B", "A", "B", "C"}}
POSTED BY: David Keith
POSTED BY: Oscar Rodriguez
Posted 10 years ago

Oscar,

I'm not sure I understand but if you want only a particular range of rows for a given column, it can be done like this in V10:

In[3]:= d = Table[10 j + i, {j, 9}, {i, 5}]

Out[3]= {{11, 12, 13, 14, 15}, {21, 22, 23, 24, 25}, {31, 32, 33, 34, 
  35}, {41, 42, 43, 44, 45}, {51, 52, 53, 54, 55}, {61, 62, 63, 64, 
  65}, {71, 72, 73, 74, 75}, {81, 82, 83, 84, 85}, {91, 92, 93, 94, 
  95}}

In[4]:= d[[3 ;; 5, 2]]

Out[4]= {32, 42, 52}
POSTED BY: David Keith
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard