Message Boards Message Boards

[✓] Select specific data from a list?

GROUPS:

Hello Wolfram community,

I'm currently trying to analyze the relationship between the fuel type of a car and the distance that was driven with it. To do so, I have transposed two columns from a data set. My next step is to visualize this in some form by separating it by fuel type and showing the mean distance traveled with that type of car.

To do so however, I need to select specific data from the transposed data set. I started making a Select Function, but kept receiving error message. My list of data looks as follows:

{{"Electricity", 34} , {"Diesel",10} , {"Plug In",49} , {......}}

If I wanted to select only the combinations with "Electricity," how would I go about this?

Any help is greatly appreciated! Cheers!

POSTED BY: Joana Schenk
Answer
4 months ago

What about this?

fueltype = {"Super (E10)", "Diesel", "Electricity", "Petrol", "Plug In"};
data = Transpose[{RandomChoice[fueltype, 20], RandomInteger[80, 20]}];

This gives something like:

enter image description here

Now,

Select[data, #[[1]] == "Diesel" &]

should help.

Cheers,

Marco

POSTED BY: Marco Thiel
Answer
4 months ago

This is what Cases is made for.

In[11]:= data = Table[{RandomChoice@{"E", "D", "P"}, RandomInteger@100}, 20]

Out[11]= {{"D", 25}, {"D", 4}, {"D", 55}, {"E", 29}, {"E", 8}, {"D", 
  19}, {"P", 17}, {"P", 42}, {"D", 65}, {"D", 13}, {"E", 53}, {"D", 
  30}, {"D", 98}, {"E", 83}, {"E", 28}, {"D", 7}, {"E", 11}, {"E", 
  86}, {"P", 11}, {"E", 82}}

In[12]:= Cases[data, {"E", _}]

Out[12]= {{"E", 29}, {"E", 8}, {"E", 53}, {"E", 83}, {"E", 28}, {"E", 
  11}, {"E", 86}, {"E", 82}}
POSTED BY: Hans Milton
Answer
4 months ago

Thank you Hans and Marco for your help!

I was able to select the data so that just the data shows up for each fuel type. How would I be able to just select the kilometers driven (i.e. the integer values) which are linked to the fuel types?

I'm asking because I would like to have a list of just the kilometers driven for each fuel type and from there calculate the mean driven for each fuel type.

POSTED BY: Joana Schenk
Answer
4 months ago

This is starting to smell like school homework! I might be wrong. Could you show what you have done this far?

Anyway, it seems that you are looking to extract the 2nd column from a n by 2 matrix.

The function Part will fix it. Check out the documentation: Getting and Setting Pieces of Matrices

POSTED BY: Hans Milton
Answer
4 months ago

Thanks for your help! After trying around and asking for help I was able to figure it out. Thank you!

POSTED BY: Joana Schenk
Answer
4 months ago

Group Abstract Group Abstract