How can I rearrange a table with different types of data?

2 visualizaciones (últimos 30 días)
Igor
Igor el 10 de Oct. de 2019
Comentada: Star Strider el 11 de Oct. de 2019
I've never worked with tables so I find this very confusing. I have some survey data that I need to rearrange. The data has unique id's and medication that respondents take. The current form is that each row is a medication. I would like to create a data set where each row is unique patient id's with columns containing data on medications they take.
The current form of the data is:
ID Medication
1 X
1 Y
1 Z
2 T
3 A
3 B
And I'd like it to be
ID Med1 Med2 Med3
1 X Y Z
2 T
3 A B
I started off by creating a table of the necessary dimensions, creating matrixes of zeros for the parts of the table that have numeric data, and empty strings for the part of the table that have strings (drug names).That did creat a table of the right dimensions. Sort of. The tabel that I get has the right number of colums, but each chunk of string columns is treated as one, so then when I start indexing to fill the table, the code takes the first drug name and instead of putting it into position (1,2), it fills the whole row of string columns with it. I also can't figure out a way how to rename string columns in the table... I'd be very grateful for any suggestions. Thanks!
% Creating the tabel
c1 = array2table(zeros(e,1));
c1.Properties.VariableNames{'Var1'} = 'ID';
c27 = (strings(e,198));
c811 = array2table(zeros(e,132));
c12 = strings(e,33);
data = horzcat(c1,table(c27),c811,table(c12));
% Populating the empty data set
r = 1;
s = 1;
v = 0;
for k = 1:e
p = B.ID(r,1);
m = find(B.ID == p);
n = length(m);
data(k,1) = num2cell(p);
for q = 2:h
for u = 1:n
aa = u + v;
bb = u + s;
ww = cell2table(B{(aa),q})
data(k,(bb)) = ww.Var1;
end
s = i*q; % Number of empty columns
end
r = r + n;
v = n;
end

Respuestas (1)

Star Strider
Star Strider el 10 de Oct. de 2019
See if the unstack function will do what you want.
  2 comentarios
Igor
Igor el 11 de Oct. de 2019
Thank you very much for your response. I think unstack could work in theory, although I must say I can't figure out how to use it in this particular situation.
if I just use A = unstack(B,'Medication','ID')
I am prompted to specify AggregationFunction because I have multiple rows of non-numeric data, but I can't find a list of possible aggregation funcions to choose from. And I am actually not sure what it wants to aggregate. My original dataset has 12 variables, so all other variables are supposed to be grouping variables. Is that what Matlab wants to aggregate?
thank you
Star Strider
Star Strider el 11 de Oct. de 2019
My pleasure.
It is difficult for me to follow what you are doing. The AggregationFunction is an optional name-value pair argument, so the function can likely be anything you want that meets the requirements of unstack.
I cannot find anything in the unstack documentation that deals with anything other than numeric variables. One possible work-around would be to assign numeric variables to the different medications, avoiding the AggregationFunction requirement.

Iniciar sesión para comentar.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by