How to divide a column in table to other variables?
16 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Behrooz Daneshian
le 12 Jan 2023
Modifié(e) : Stephen23
le 13 Jan 2023
Hi all
I have a table called "TEMP" reporting daily minimum temprature(TMIN), daily maximum temprature(TMAX), and daily average temprature(TAVG) for a specific location in 2004. I want to devide the 'datatype' column into 3 columns of TMIN, TMAX, and TAVG. I will do it using the following code, but I would see 'NaN' values in the new table. Can anyone help me with this regard?
load ("TEMP1.mat");
unstackedTemp = unstack(TEMP,"value","datatype");
Réponse acceptée
Stephen23
le 13 Jan 2023
Modifié(e) : Stephen23
le 13 Jan 2023
You need to specify the "GROUPINGVARIABLE" option, otherwise "...unstack treats the remaining variables as grouping variables. Each unique combination of values in the grouping variables identifies a group of rows in S that is unstacked into a single row of U." So while you might think that every three lines of TAVG, TMAX, and TMIN form a "set" of values, take a look at the column/variable "attributes": are they the same for each set? (hint: no)
Solution: specify the grouping variable to specify which data get merged onto one line.
S = load('TEMP1.mat')
T = S.TEMP
T = unstack(T,"value","datatype", "GroupingVariable","date")
As an aside, note that the "date" column/variable should probably be DATETIME, not text. This can be fixed using CONVERTVARS (even better: fix this when importing the data):
T = convertvars(T,'date','datetime')
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Tables dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!