Splitting a Table at Every N Rows

I have a single table with 58 years worth of tax data for 75 different countries. It is currently organized with the countries stacked on top of each other alphabetically. Hence, I have 58 years of data for Argentina, followed by 58 years of data for Austria, and so on. I would like to split it at every 58th row, so that I can store each country's data in its own separate table. Is there a quick way I can do this?

 Accepted Answer

You can use this script to split your table as mentioned in the question. Replace myTable with the name of your table. It will create a cell array seperatedCountries.
nunberCountries = 75;
noumberYears = 58;
rowOndexVector = repelem(1:nunberCountries, noumberYears);
seperatedCountries = splitapply(@(x) {x}, cell2mat(myTable{:,:}), findgroups(rowOndexVector));
seperatedCountries{1} will contain data about first country (i.e. Argentina). Similarly seperatedCountries{1} will contain data about second country (i.e. Austria) as so on.

8 Comments

I get the error 'Unable to concatenate the table variables 'country' and 'year', because their types are cell and double' when I run the last line.

My bad! I just assumed that all your table entries are double. For a more general case please replace the last two lines as

rowOndexVector = repelem(1:nunberCountries, noumberYears)';
seperatedCountries = splitapply(@(x) {cell2table(x)}, myTable{:,:}, findgroups(rowOndexVector));
I still get the same error.
If possible, can you show us a sample of your table? Or can you create a table with similar fields? It will help in understanding the issue.

<<

>> That's a part of the table. At row 58, the country name changes. The rest of the table repeats those years and those variables for all the other countries.

Please check this now

rowOndexVector = repelem(1:nunberCountries, noumberYears)';
seperatedCountries = splitapply(@(x) {cell2table(x)}, table2cell(myTable), findgroups(rowOndexVector));
The result after running this was a 75x1 cell called separatedCountries with each row of the cell containing a table. From there, I used this code to get it into a structure:
for i = 1:numberCountries
my_field = strcat('v',num2str(i));
variable.(my_field) = separatedCountries{i};
end
I think from here, I should leave it as a structure and nest the country groups in the structure and plot from there. Thank you!
You are welcome.

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 23 Apr 2018
Depending on what you want to do with those country-specific table arrays, if you're using release R2018a or later you should look at the groupsummary function to see if it can let you avoid creating a large number of small arrays.

3 Comments

Even in earlier versions, I would recommend not splitting the table into smaller tables.

tables have functions that let you perform calculations by group, e.g. by country. For example, to calculate the mean of some columns per country, you'd do:

varfun(@mean, yourtable, 'GroupingVariables', 'Country', 'InputVariable', {list_of_variables_to_average})

In general, it is better to keep data together in the same variable and use indexing or filtering to get what you want rather than splitting it into multiple variables.

I have considered do that. My main aim is to plot the data that I have. The problem is that varfun can only plot all the 75 countries data together, which won't be very meaningful for comparisons or easy to read off of. If there is a way to group each countries data together and then plot only a certain grouping of those countries (like say, only the EU or only OECD countries) with varfun such that I need not separate them and also such that I can call on both the groupings and the individual countries as needed for later data manipulation, then I would do that.

"The problem is that varfun can only plot all the 75 countries data together"

varfun can do anything you want. The function you pass to varfun can be as complex as you wish.

"only a certain grouping of those countries (like say, only the EU or only OECD countries)"

Add a column to your table for these categories, if it does not exist already. You can then use the 'GroupingVariables' option of varfun.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!