Converting rows to columns

조회 수: 23 (최근 30일)
Gorkem Akgul
Gorkem Akgul 2021년 3월 27일
편집: Gorkem Akgul 2021년 3월 29일
I would like to convert Date rows to column and add active numbers for each Date column.
So there will be 14 columns of dates and each coulmn is supposed to keep actives for each country.
I've spent some time trying to sort this out yet there needs to be used so many for loops and manipulation.
Is there any function that i can do it easily ?
This is the code that i used to create the table above.
tbl=table(gs.Country,gs.sum_sum_Active,gs.Date,'VariableNames',{'Country','Active','Date'})

답변 (1개)

Cris LaPierre
Cris LaPierre 2021년 3월 28일
You'll have to be careful. Each column of a table can be a different data type, but all data in a column must be of the same data type. You can make your variables (column names) be dates, but it is likely going to be non-trivial to rearrange your data.
One thought might be to use groupsummary to arrange the data so that each country has the same number of dates.
sumTbl = groupsummary(tbl,["Country","Date"],'mean','IncludeEmptyGroups',true,'IncludeMissingGroups',true)
Ideally each group should have a groupcount of 1, but I specified 'mean' as the way to combine mulitple values.
To do more, I think we would need to see your data to be more specific.
  댓글 수: 1
Gorkem Akgul
Gorkem Akgul 2021년 3월 29일
편집: Gorkem Akgul 2021년 3월 29일
Thank you Cris LaPierre,
I'm gonna share the codes that i used to create the table i want. Perhaps that could help some other people who would like to do sth like that.
% I firstly create a summary table of active cases for each country and
% cases.
ppdata=alldatasummary(1,:);
for i=1:10
ppdata = [ppdata; alldatasummary(alldatasummary.Country==tenmostactivesummary_actives.Country(i),:)];
end
ppdata.Date=dateshift(ppdata.Date,"start","Month");
ppdata.Date.Format='dd-MMM-yyyy';
ppdata=ppdata(2:end,:);
ppdata.Country=removecats(ppdata.Country);
ppsummary=groupsummary(ppdata,["Country","Date"],"sum","sum_Active")
% Then i start transforming this table to a proper way that can be used for
% paralellplot
rownumber=numel(ppsummary(ppsummary.Country=='Belgium',1));
ppsummary.Date=string(ppsummary.Date)
ppsummary.Country=string(ppsummary.Country)
arraysummary=table2array(ppsummary)
% creating the first part of the table
for i=1:rownumber+1
if(i==1)
arraydates(i,1)="Country";
else
arraydates(i,1)=arraysummary((i-1),2)
end
end
arraydates=arraydates(:,1)';
%% creating the second part of the table
activesum=arraysummary(:,4);
activesum = reshape(activesum,[rownumber,10]);
activesumtranspose=activesum';
arraycon=unique(arraysummary(:,1));
activesumtranspose=[arraycon activesumtranspose];
summarytbl2=array2table(activesumtranspose)
% checking if any variablename = missing
for i=1:rownumber+1
if(ismissing(arraydates(i))==1)
arraydates(i)={'Unknown Date'};
end
end
arraydates
% assigning proper variable names and changing variable types
summarytbl2.Properties.VariableNames=cellstr(arraydates);
summarytbl2.Country=categorical(summarytbl2.Country);
for i=2:rownumber+1
summarytbl2.(i) = double(summarytbl2.(i));
end
summarytbl2

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

카테고리

Help CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by