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.