I have a dataframe such as
Groups Names
G1 SP1
G1 SP2
G1 SP3
G2 SP1
G2 SP4
G3 SP2
G3 SP1
And I would like to transform it as :
Names G1 G2 G3
SP1 1 1 1
SP2 1 0 1
SP3 1 0 0
SP4 0 1 0
Where in columns are the Groups
and within cell 1 = present and 0 = absent
Here is the dput format
structure(list(Groups = c("G1", "G1", "G1", "G2", "G2", "G3",
"G3"), Names = c("SP1", "SP2", "SP3", "SP1", "SP4", "SP2", "SP1"
)), class = "data.frame", row.names = c(NA, -7L))
>Solution :
Use table:
table(df$Names, df$Groups)
G1 G2 G3
SP1 1 1 1
SP2 1 0 1
SP3 1 0 0
SP4 0 1 0