How to duplicate certain rows of a dataset (class = dataset)?
6 views (last 30 days)
Show older comments
I have a dataset with 200 rows and 20 columns. I want to duplicate each row that has a depth value of 10, e.g. dataset.depth == 10, and place the duplicate row in the next row. (The reason for duplication is because I will change values in 2 of the columns of this duplicated row, but most of the column values will remain the same.) I am thinking that a for loop with an if/else statement could work.
for i=1:length(dataset.depth)
if dataset.depth(i) == 10
DUPLICATE COMMAND HERE NEEDED
end
end
Thanks!
2 Comments
Answers (2)
Peter Perkins
on 23 Oct 2012
As Matt says, no need to use loops. This might be a little simpler.
First cook up some data:
aa = dataset((1:100)',randi(10,100,1),randn(100,1),'VarNames',{'dpID' 'depth' 'somethingElse'});
Then find the rows to duplicate, and put duplicates at the end:
i = (aa.depth == 10);
Then sort to put duplicates in place:
aa = sortrows([aa; aa(i,:)],'dpID');
Hope this helps.
0 Comments
See Also
Categories
Find more on Data Types in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!