Message Boards Message Boards


[✓] Select specific data from a list?

Posted 10 months ago
5 Replies
3 Total Likes

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!

5 Replies

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


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

should help.



Posted 10 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 10 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 10 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 10 months ago

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

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract