I have a table with mass spectrometry data from some morphospecies of plants, using different techniques.

sp  technique   mz  abundance
sp1 ESIneg  118.89  3.01
sp1 ESIneg  172.72  3.20
sp1 ESIneg  202.94  3.80
sp1 ESIpos  118.30  2.59
sp1 ESIpos  170.68  3.13
sp1 ESIpos  257.97  3.28
sp2 ESIneg  132.33  22.22
sp2 ESIneg  211.84  3.87

I want to use some sort of cluster analysis to test if these species are the same or different. I've been reading different papers, but they don't show their data table, only the sort of analysis done (Kmeans, Ward, Model based...), most times in proprietary software.

Most tutorials about cluster analysis start with a XY table, which is not the case here.

I want to do it in R, I know there are LOTS of packages available, so maybe someone could point me to a more didactic paper or tutorial, or perhaps a specific package/function? And is it possible to use all different techniques at once, or will I have to analyze each one separately?


I think what you are missing is the creation of dummy variables.

You can expand sp and technique columns into dummy variables {0,1}

sp  technique   mz  abundance
sp1 ESIneg  118.89  3.01


sp1 sp2 ... spN ESIng ESIpos mz      abundance
1   0       0   1     0      118.89  3.01

After that you can use any of the clustering algorithms you'd like. k-means in the R stats package would work here.

There is a built-in R function model.matrix that can automate the creation of the dummy variables.

I think the confusion with the XY table is because XY tables are easy to visualize. You can pick any two variables or three and make a plot and then label the clusters. Keep in mind that there are dimensions that have been flattened and it might not look correct to the naked eye. Something like t-SNE can help reduce the dimensions making it more "plottable".

*Note: Consider scaling your variables before clustering.

  • $\begingroup$ Thank you, I'm trying it. But the use of model.matrix is not trivial. 'db' is my data.frame. If I do db1 <- model.matrix(~ sp + technique + mz + abundance, db), I get a numeric matrix with the same number of rows and 16 cols. All species are there, but one of the techniques is missing. Any suggestion? $\endgroup$ – Rodrigo Jan 11 '17 at 14:03
  • $\begingroup$ It seems I have a solution here: stackoverflow.com/questions/4560459/… $\endgroup$ – Rodrigo Jan 11 '17 at 14:05
  • $\begingroup$ Well, I'm still far from the kind of clustering I want. I asked a new question: stats.stackexchange.com/questions/255675/… $\endgroup$ – Rodrigo Jan 11 '17 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.