Help reorganizing data in a table
显示 更早的评论
Hi,
I have an excel sheet with lots of data based on recordings and measurements. These recordings and measurements are displayed in the excel sheet in a certain way. I need to reorganise this data and display it in another way. I have attached an example excel sheet with two days worth of data. The attached excel sheet also shows how I need the data to be displayed.
I have been trying to use MATLAB for nearly a week to try to get this working but I don't think I am any closer to figuring it out. I am new to MATLAB and don't have much experience using it for something like this.
I would really appreciate it if someone could explain to me in simple terms how to do this. I have lots of data to work with so doing this manually is not an option.
Andrew
2 个评论
the cyclist
2021-8-5
@Konrad beat me to posting the elegant unstack solution. I'll just comment here that in general these types of operations are known as "pivoting" the table. You'll see various terminology out there (when considering other languages, such as R and python) such as making the table "long" or "wide", etc.
One other comment. Are you really sure that you want the new format? The reason I ask is that the original seems to me to be closer to the "tidy" format (see, e.g. this article) that is typically best for analysis. But I have to admit I did not think deeply about this yet. Maybe the new format is actually tidier.
Regardless, you may find the article informative. I like to evangelize about tidy data, as I find it to be a powerful concept.
Peter Perkins
2021-8-5
Andrew, this is essentially the same question you asked earlier: unstacking multiple variables. I recommend you look at the answer there.
采纳的回答
更多回答(1 个)
Hi Andrew,
I think unstack() is what you're looking for:
T=readtable('example_sheet.xlsx');
uT = unstack(T,{'Duration' 'Rating' 'Measurement1' 'Measurement2'},'Category');
Best, Konrad
5 个评论
ANDREW Feenan
2021-8-5
ANDREW Feenan
2021-8-5
编辑:ANDREW Feenan
2021-8-5
Do you want to reorder the columns?
from
Duration_Activity1 Duration_Activity2 Rating_Activity1 ...
to
Duration_Activity1 Rating_Activity1 Measurement1_Activity1 Measurement2_Activity1 Duration_Activity2 ...
?
You could do something like this:
uT = unstack(T,{'Duration' 'Rating' 'Measurement1' 'Measurement2'},'Category');
nActivity = 2;
nVars = 4;
nIdCols = 3;
cIdx = bsxfun(@plus,((1:nActivity:nActivity*nVars)+nIdCols).',(1:nActivity)-1);
uTnew = uT(:,[1:nIdCols cIdx(:).']);
I think it's the same approach, but you could just use indexing.
T=readtable('example_sheet.xlsx');
newT = unstack(T,{'Duration' 'Rating' 'Measurement1' 'Measurement2'},'Category');
newT = newT(:,[1:4 6 8 4 6 10 5 7]);
% name variables
newT.Properties.VariableNames(4:6) = "Activity 1: "+T.Properties.VariableNames(5:7);
newT.Properties.VariableNames(7:9) = "Activity 1b: "+T.Properties.VariableNames(5:7);
newT.Properties.VariableNames(10:11) = "Activity 2: "+T.Properties.VariableNames(5:6)
Konrad
2021-8-6
Just to promote regular expressions (and because I love regexp, I admit):
The desired column names can be achieved in one line:
uT.Properties.VariableNames = regexprep(uT.Properties.VariableNames, '(^[^_]+)_([^_]+)$', '$2: $1')
类别
在 帮助中心 和 File Exchange 中查找有关 Data Import from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!