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?

 채택된 답변

Ameer Hamza
Ameer Hamza 2018년 4월 23일

1 개 추천

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

Kenneth Mano
Kenneth Mano 2018년 4월 24일
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));
Kenneth Mano
Kenneth Mano 2018년 4월 24일
I still get the same error.
Ameer Hamza
Ameer Hamza 2018년 4월 24일
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.
Kenneth Mano
Kenneth Mano 2018년 4월 24일

<<

>> 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!
Ameer Hamza
Ameer Hamza 2018년 4월 25일
You are welcome.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2018년 4월 23일

2 개 추천

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

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.

Kenneth Mano
Kenneth Mano 2018년 4월 25일
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.
Guillaume
Guillaume 2018년 4월 25일

"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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터 및 File Exchange에서 Tables에 대해 자세히 알아보기

질문:

2018년 4월 23일

댓글:

2018년 4월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by