Effacer les filtres
Effacer les filtres

Restructuring a matrix based on an index

3 vues (au cours des 30 derniers jours)
Corymbiamaculata
Corymbiamaculata le 8 Juin 2023
Commenté : Cris LaPierre le 9 Juin 2023
I have a 60x12 matrix in a table format (with column headers), where the first three columns are identification indices: site (1, 2, 3), species (1, 2, 3), and individual ID (1, 2, ...10), while the remaining nine columns are measured data (height, diameter etc.). I want to restructure the matrix so that site and species remain as columns, whereas the individual ID (within each combination of site and species) is used as an index in the nine columns with measured variables (which would turn into 9 * 10). So instead of height, diameter etc., I would get height_ID1, ... height_ID10, diameter_ID1, ... diameter_ID10 etc. What complicates things (perhaps) is that:
1) for each site and species, individual IDs are 1, 2, ...10. I want to unstack data based on individual IDs within each combination of species and site (not aggregate them together), i.e. keep all combinations of site and species in columns 1 and 2,
2) in some cases, there are two or more rows with the same combination of site, species and individual ID, e.g. in case of diameter which was measured in two directions, or other parameters which were sampled a few times.
Is there an easy function which will let me restructure my data in such a way, and adjust variable names accordingly (by combining original variable name with ID+number, i.e. height_ID1)?
Many thanks in advance for help!
  3 commentaires
Corymbiamaculata
Corymbiamaculata le 8 Juin 2023
Thanks! I tried using it, but for some reason some of the combinations of site and species are lost in the unstacked table. Do you know if it's possible to specify variables (i.e. site and species) which should remain... unstacked?
Cris LaPierre
Cris LaPierre le 9 Juin 2023
There is not an option for that. A workaround might be to create a new table variable that does create a unique value for each row you want to keep, and use that to unstack your table.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by