How to divide a column in table to other variables?
13 次查看(过去 30 天)
显示 更早的评论
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");
采纳的回答
Stephen23
2023-1-13
编辑:Stephen23
2023-1-13
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 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!