Converting rows to columns
21 次查看(过去 30 天)
显示 更早的评论
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'})
0 个评论
回答(1 个)
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!