How to divide a column in table to other variables?
조회 수: 16 (최근 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개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!