Group Abstract Group Abstract

Message Boards Message Boards

0
|
2.8K Views
|
2 Replies
|
1 Total Like
View groups...
Share
Share this post:

How extract elements from data table and recode into zero and one

Hi everyone,
I have a dataset consisting of species and color1, color2, color3, color4 (as in the notebook) It should look like this

{{"Species", "Color1", "Color2", "Color3", "Color4"}, {"species B", 
  "black", "red", "blue", "pink"}, {"species A", "yellow", "black", 
  "white", "orange"}, {"species c", "red", "indigo", "cream", 
  "yellow"}}

What I am trying to do is extract the list of colors available, transpose it as a new headers and map it to species. If the species has the color it is coded as 1 and zero if it doesn't.

Ideally it should look like this

{{"Species", "black", "blue", "cream", "indigo", "orange", "pink", "red", "white", "yellow"},
 {"species B", 1, 1, 0, 0, 0, 0, 1, 0},
 {"species A", 0, 0, 0, 0, 1, 0, 1, 1},
 {"species c", 0, 0, 1, 1, 0, 0, 1, 0}}

I've tried using this line of code

uniqueColors = Union[Flatten[data[[All, 2 ;; 5]]]]
newList = Table[{data[[i, 1]], If[MemberQ[colors[[i]], uniqueColors[[1]]], 1, 0], If[MemberQ[colors[[i]], uniqueColors[[2]]], 1, 0], If[MemberQ[colors[[i]], uniqueColors[[3]]], 1, 0], If[MemberQ[colors[[i]], uniqueColors[[4]]], 1, 0], If[MemberQ[colors[[i]], uniqueColors[[5]]], 1, 0], If[MemberQ[colors[[i]], uniqueColors[[6]]], 1, 0], If[MemberQ[colors[[i]], uniqueColors[[7]]], 1, 0], If[MemberQ[colors[[i]], uniqueColors[[8]]], 1, 0], If[MemberQ[colors[[i]], uniqueColors[[9]]], 1, 0]}, {i, 1, Length[data]}]
newList = Prepend[newList, Prepend[uniqueColors, "Species"]]

But the output is not what I expected

{{"Species", "black", "blue", "cream", "indigo", "orange", "pink", 
  "red", "white", "yellow"}, {"Species", 1, 1, 0, 0, 0, 1, 1, 0, 
  0}, {"species B", 1, 0, 0, 0, 1, 0, 0, 1, 1}, {"species A", 0, 0, 1,
   1, 0, 0, 1, 0, 1}, {"species c", 0, 0, 0, 0, 0, 0, 0, 0, 0}}

I'd really appreciate if anyone could help me on this.

2 Replies

Hi Eric,

Thanks for this, I actually figured out what went wrong with my original code:

newList = 
 Table[{species[[i]], (*This is the part that I messed up*)
   If[MemberQ[colors[[i]], uniqueColors[[1]]], 1, 0], 
   If[MemberQ[colors[[i]], uniqueColors[[2]]], 1, 0], 
   If[MemberQ[colors[[i]], uniqueColors[[3]]], 1, 0], 
   If[MemberQ[colors[[i]], uniqueColors[[4]]], 1, 0], 
   If[MemberQ[colors[[i]], uniqueColors[[5]]], 1, 0], 
   If[MemberQ[colors[[i]], uniqueColors[[6]]], 1, 0], 
   If[MemberQ[colors[[i]], uniqueColors[[7]]], 1, 0], 
   If[MemberQ[colors[[i]], uniqueColors[[8]]], 1, 0], 
   If[MemberQ[colors[[i]], uniqueColors[[9]]], 1, 0]}, {i, 1, 
   Length[species]}]
newList = Prepend[newList, Prepend[uniqueColors, "Species"]]

However, your code is way cleaner and more convenient. This is just a test dataset, I am actually looking into different flower colors and the associated pollinators. So later on I need to merge the colors database with the pollinator type. I am planning to use FreeViz in Orange3 (there is example attached). So basically I'm trying to look at the association between different colors and different types of pollinators. I am not certain whether Mathematica has the same equivalent. Your code is far better since I have about 40 color schemes, if using my own code that means repeating the If command about 40 times.

Attachment

Attachments:
Posted 2 years ago

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