Dataset reshape and create new columns

2 次查看(过去 30 天)
Hello all,
I am have problems with dataset reshaping and wrangling. I have a file with dataset in the form as shown in the example data attached, every 4 columns are in one group, the first row is the sample ID, and the second row contains the information of the variables. I am wondering whether there are any ways to concatenate all the groups vertically, and create new column names with the information from the sample ID (specifically, text before underscore as "SampleType", and that after the underscore as "replicateID"). In the end the output dataset should have the original 4 columns and the newly added two columns.
Thank you very much for the help!

采纳的回答

Voss
Voss 2022-3-30
Here's one way to do it:
% read the file:
data = readcell('example_data.csv');
data(1:10,:)
ans = 10×8 cell array
{'nonyeasted_19'} {'nonyeasted_19'} {'nonyeasted_19'} {'nonyeasted_19'} {'yeasted_01'} {'yeasted_01' } {'yeasted_01'} {'yeasted_01'} {'Force (N)' } {'Distance (m)' } {'Time (sec)' } {'Status' } {'Force (N)' } {'Distance (m)'} {'Time (sec)'} {'Status' } {[ 0]} {[ 0]} {[ 0]} {[ 101]} {[ 0]} {[ 0]} {[ 0]} {[ 101]} {[ -0.0077]} {[ 0]} {[ 0.0020]} {[ 1]} {[ 0.0024]} {[ 0]} {[ 0.0020]} {[ 1]} {[ 0.0023]} {[ 0]} {[ 0.0040]} {[ 1]} {[ 0.0307]} {[ 0]} {[ 0.0040]} {[ 1]} {[ -0.0707]} {[ 0]} {[ 0.0060]} {[ 1]} {[ -0.0487]} {[ 0]} {[ 0.0060]} {[ 1]} {[ -0.2155]} {[ 0]} {[ 0.0080]} {[ 1]} {[ -0.2063]} {[ 0]} {[ 0.0080]} {[ 1]} {[ -0.2026]} {[ 0]} {[ 0.0100]} {[ 1]} {[ -0.1928]} {[ 0]} {[ 0.0100]} {[ 1]} {[ -0.0628]} {[ 2.0000e-06]} {[ 0.0120]} {[ 1]} {[ -0.0421]} {[ 0]} {[ 0.0120]} {[ 1]} {[ -0.0481]} {[ 4.0000e-06]} {[ 0.0140]} {[ 1]} {[ -0.0278]} {[ 1.0000e-06]} {[ 0.0140]} {[ 1]}
% get the types and ids:
C = regexp(data(1,:),'(.+)_(.+)','tokens');
C = [C{:}];
type_id = vertcat(C{:})
type_id = 8×2 cell array
{'nonyeasted'} {'19'} {'nonyeasted'} {'19'} {'nonyeasted'} {'19'} {'nonyeasted'} {'19'} {'yeasted' } {'01'} {'yeasted' } {'01'} {'yeasted' } {'01'} {'yeasted' } {'01'}
% do the reshaping and column-prepending operations:
[nrows,ncols] = size(data);
new_data = ['SampleType' 'replicateID' data(2,1:4); ... % first row: variable names
repelem(type_id,(nrows-2)/4,1) ... % first two columns: types and IDs
reshape(permute(reshape(data(3:end,:),[],4,ncols/4),[1 3 2]),[],4)]; % columns 3-6: numeric data cells
% fill "missing" cells with a scalar NaN value:
new_data(cellfun(@(x)isa(x,'missing'),new_data)) = {NaN};
new_data(1:10,:) % check the top
ans = 10×6 cell array
{'SampleType'} {'replicateID'} {'Force (N)'} {'Distance (m)'} {'Time (sec)'} {'Status'} {'nonyeasted'} {'19' } {[ 0]} {[ 0]} {[ 0]} {[ 101]} {'nonyeasted'} {'19' } {[ -0.0077]} {[ 0]} {[ 0.0020]} {[ 1]} {'nonyeasted'} {'19' } {[ 0.0023]} {[ 0]} {[ 0.0040]} {[ 1]} {'nonyeasted'} {'19' } {[ -0.0707]} {[ 0]} {[ 0.0060]} {[ 1]} {'nonyeasted'} {'19' } {[ -0.2155]} {[ 0]} {[ 0.0080]} {[ 1]} {'nonyeasted'} {'19' } {[ -0.2026]} {[ 0]} {[ 0.0100]} {[ 1]} {'nonyeasted'} {'19' } {[ -0.0628]} {[ 2.0000e-06]} {[ 0.0120]} {[ 1]} {'nonyeasted'} {'19' } {[ -0.0481]} {[ 4.0000e-06]} {[ 0.0140]} {[ 1]} {'nonyeasted'} {'19' } {[ -0.0601]} {[ 6.0000e-06]} {[ 0.0160]} {[ 1]}
new_data(nrows+(-5:4),:) % check the transition from nonyeasted to yeasted
ans = 10×6 cell array
{'nonyeasted'} {'19'} {[-0.2483]} {[-0.0809]} {[90.6540]} {[ 1]} {'nonyeasted'} {'19'} {[-0.2420]} {[-0.0809]} {[90.6560]} {[ 1]} {'nonyeasted'} {'19'} {[-0.2355]} {[-0.0809]} {[90.6580]} {[ 1]} {'nonyeasted'} {'19'} {[-0.2441]} {[-0.0809]} {[90.6600]} {[ 1]} {'nonyeasted'} {'19'} {[-0.2484]} {[-0.0809]} {[90.6620]} {[ 1]} {'yeasted' } {'01'} {[ 0]} {[ 0]} {[ 0]} {[101]} {'yeasted' } {'01'} {[ 0.0024]} {[ 0]} {[ 0.0020]} {[ 1]} {'yeasted' } {'01'} {[ 0.0307]} {[ 0]} {[ 0.0040]} {[ 1]} {'yeasted' } {'01'} {[-0.0487]} {[ 0]} {[ 0.0060]} {[ 1]} {'yeasted' } {'01'} {[-0.2063]} {[ 0]} {[ 0.0080]} {[ 1]}
% write the new file:
writecell(new_data,'example_data_modified.csv');
Read the documentation for reshape, permute, and repelem to understand how they are used here.

更多回答(0 个)

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by