Rearranging rows side by side based on a column value
Afficher commentaires plus anciens
Hi,
I have an excel file with 300k samples (rows) and 40 columns. The first column is ID which has duplicate values and last column is about status and has binary values either 0 or 1.
I am looking to scan through this file and if the status column of first row is 0 it should copy the next row columns from 2 to 39 (excluising ID and status) and paste it where first row ends first row if the belong to same ID. This should happen for every other row with status 0 and it should copy only data related to same ID. Please see example below. From the expected output you can obvserve for ID 35 we didn't append anyt value for first sample as the status is 1 and for ID 35 the third sample is also not appended even if status is 0 as its the last row related to 35 and we cannot append ID 45 values,
ID Col1 Col2 Col3 Col4 Status
35 993 65 130 0 1
35 993 65 24 1 0
35 993 65 7 1 0
45 993 65 9 1 0
45 993 65 19 1 0
45 993 65 58 0 0
Expected Output:
ID Col1 Col2 Col3 Col 4 Status Col1 Col2 Col3 Col4
35 993 65 130 0 1
35 993 65 24 1 0 993 65 7 1
35 993 65 7 1 0
45 993 65 9 1 0 993 65 19 1
45 993 65 19 1 0 993 65 58 0
45 993 65 58 0 0
Thanks
4 commentaires
Guillaume
le 7 Fév 2019
Can you attach an example excel file (with a bit more rows than your example), and ideally a 2nd file which shows the desired output.
Is the header part of the file (would make it easier to read as a table)?
Sunny
le 7 Fév 2019
Guillaume
le 8 Fév 2019
Oh, I didn't realise you wanted the filtered columns to be appended to the right of the same file. While it's perfectly doable, are you really sure you want this? I wouldn't think that repeated data and data with gaps in the rows is very practical? Wouldn't you rather have it as a separate file (with no gaps)?
As to the headers, it's up to you if you want them or not. It's a slightly different approach (table vs matrix) but the same amount of code either way.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Data Type Identification dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!