Copy data from rows to columns in another table

I currently have a table that looks like this:
geo_code data_name data_value
Region_x Population 216555
Region_x Total Income 11116634370
Region_x Median Income 50321
Region_y Population 145003
Region_x Total Income 4952577465
Region_x Median Income 32154
I need a table that looks like this:
geo_code Population Median Income Average Income
Region_x 216555 50321 51334
Region_y 145003 32154 34155
Where average income is "Total Income" divided by "Population"
Any idea how I would get there? This is just an example, the data I'm working with has over 4000 unique geo codes and over 3.5 million data points. Thanks in advance.

 Accepted Answer

Assuming your original data is in a MATLAB table, the 'unstack' function will get you most of the way there. There would just be some simple cleanup to get the final numbers you want.
newTbl = unstack(origTbl,"data_value","data_name")
newTbl =
geo_code MedianIncome Population TotalIncome
__________ ____________ __________ ___________
"Region_x" 50321 2.1656e+05 1.1117e+10
"Region_y" 32154 1.45e+05 4.9526e+09

More Answers (0)

Categories

Products

Release

R2019a

Community Treasure Hunt

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

Start Hunting!