Main Content

S.tack data from multiple variables into single variable

Description

S.= stack(vars.converts the table or timetable,那into an equivalent table or timetable,S.那that is stacked. Thefunction stacks up multiple variables from那specified byvars.那into a single variable inS.。In general,S.包含更少的变量,但更多的行,而不是

输出参数,S.,包含一个新的分类变量,以指示哪个变量每一行中的堆叠数据来自。replicates data from the variables inthat are not stacked.

  • 如果is a table, then you cannot stack row names.

  • 如果is a timetable, then you cannot stack row times.

S.= stack(vars.名称那Valueconverts the table,那with additional options specified by one or more名称那Valuepair arguments.

例如,您可以为新的和堆叠变量指定变量名称

[S.iu] = stack(___also returns an index vector,iu那indicating the correspondence between rows inS.和行。您可以使用以前的任何一个输入参数。

Examples

collapse all

从三个单独的测试创建包含测试分数的表。该表格式不成功。

test1 = [93; 57; 87; 89];test2 = [89; 77; 92; 86];test3 = [95; 62; 89; 91];U =表(Test1,Test2,Test3)
你=4×3表Test1 Test2 Test3 _____ _____ _____ 93 89 95 57 77 62 87 92 89 89 86 91

The table contains four rows and three variables.

将测试分数堆叠成单个变量。

S.= stack(U,1:3)
S =12×2 tableTest1_Test2_Test3_Indicator Test1_Test2_Test3 ___________________________ _________________测试1 93的Test2 89 Test3的95测试1 57的Test2 77 Test3的62测试1 87的Test2 92 Test3的89测试1 89的Test2 86 Test3的91

S.contains twelve rows and two variables.S.is in stacked format.

分类变量,test1_test2_test3_indicator.我,确定哪些测试对应的分数n the stacked data variable,Test1_Test2_Test3

Create a timetable indicating the amount of snowfall in three towns from five different storms. Specify the dates of the storms asdatetimevalues and use them as the row times of the timetable。指定Storm Numbers数组,风暴,作为一个分类阵列,因为该时间表中存在一组固定的风暴编号。

Storm =分类([1; 2; 3; 4; 5]);date = datetime({'2011-12-25';'2012-01-02';'2012-01-23';'2012-02-07';'2012-02-15'});natick = [20; 5; 13; 0; 17];波士顿= [18; 9; 21; 5; 12];伍斯特= [26; 10; 16; 3; 15];U =时间表(日期,风暴,纳尼克,波士顿,伍斯特)
你=5×4 timetable日期风暴纳尼克波士顿伍斯特___________ ____________________________________ 2011年12月25日1月28日02-JAN-2012 2 5 9 10 23-JAN-2012 3 13 21 16 07-FEB-2012 4 0 5 3 15-FEB-2012 5 17 12 15

The variables风暴andDate包含在每个位置恒定的数据。

堆叠变量NatickBoston那and伍斯特进入一个变量。命名包含堆叠数据的变量,,并命名新的指示变量,

s =堆(u,{'Natick''Boston''伍斯特'},......'NewDataVariableName''Snowfall'......'IndexVariableName''镇'
S =15×3时间表Date Storm Town Snowfall ___________ _____ _________ ________ 25-Dec-2011 1 Natick 20 25-Dec-2011 1 Boston 18 25-Dec-2011 1 Worcester 26 02-Jan-2012 2 Natick 5 02-Jan-2012 2 Boston 9 02-Jan-2012 2 Worcester 10 23-Jan-2012 3 Natick 13 23-Jan-2012 3 Boston 21 23-Jan-2012 3 Worcester 16 07-Feb-2012 4 Natick 0 07-Feb-2012 4 Boston 5 07-Feb-2012 4 Worcester 3 15-Feb-2012 5 Natick 17 15-Feb-2012 5 Boston 12 15-Feb-2012 5 Worcester 15

S.每次风暴都包含三排,重复常数变量中的数据,风暴andDate那accordingly.

分类变量,,识别哪个变量包含相应的数据。

Create a timetable containing estimated influenza rates per month along the east coast of the United States. Create a different variable for the Northeast, Mid Atlantic, and South Atlantic. Data Source: the Google Flu Trends project (since discontinued). Use adatetimearray as row times of the timetable.

月= datetime(2005,10,1,'Format''mmmm yyyy')+ calmonths(0:5);月=月;ne = [1.1902;1.3610;1.5003;1.7772;2.1350;2.2345];midatl = [1.1865;1.4120; 1.6043; 1.8830; 2.1227; 1.9920]; SAtl = [1.2730; 1.5820; 1.8625; 1.9540; 2.4803; 2.0203]; fluU = timetable(Month,NE,MidAtl,SAtl)
fluU=6×3时间表月NE MIDATL SATL _____________ ______ ______ ______ ______ ______ ______ 2005年10月1.1902 1.1865 1.273 2005年11月1.582 2005年12月1.582 2006年12月2006年12月2.135 2.1227 2.4803 2006年3月2.032

The variable包含跨行常量的数据。

堆叠变量NE米德尔那andS.Atl进入一个调用的单个变量泛滥。名称the new indicator variable地区and output an index vector,IFLUU.那to indicate the correspondence between rows in the input unstacked table,fluU那and the output stacked table,fluS

[fluS,ifluU] = stack(fluU,1:3,......'NewDataVariableName''泛骨'......'IndexVariableName''Region'
fluS=18×2时间表月地区泛滥_____________ ______ _______ October 2005 NE 1.1902 October 2005 MidAtl 1.1865 October 2005 SAtl 1.273 November 2005 NE 1.361 November 2005 MidAtl 1.412 November 2005 SAtl 1.582 December 2005 NE 1.5003 December 2005 MidAtl 1.6043 December 2005 SAtl 1.8625 January 2006 NE 1.7772 January 2006 MidAtl 1.883 January 2006 SAtl 1.954 February 2006 NE 2.135 February 2006 MidAtl 2.1227 February 2006 SAtl 2.4803 March 2006 NE 2.2345 ⋮
ifluu =18×11 1 1 2 2 2 3 3 3 4 ⋮

IFLUU.(5)is2。The fifth row in the output table,fluS,包含来自输​​入表中的第二行的数据fluU

Input Arguments

collapse all

输入表,指定为表或时间表。

Variables in堆栈,指定为正整数,正整数的向量,字符向量,字符向量,字符串阵列或逻辑向量的传染媒介。

名称-Value Pair Arguments

S.pecify optional comma-separated pairs of名称那Valuearguments.名称is the argument name andValueis the corresponding value.名称must appear inside quotes. You can specify several name and value pair arguments in any order as名称1,Value1,...,NameN,ValueN

Example:'NewDataVariableName','StackedData'names the new data variableS.tackedData

除了以外的变量vars.要包含在输出中,指定为逗号分隔对'ConstantVariables'and a positive integer, vector of positive integers, character vector, cell array of character vectors, string array, or logical vector.replicates the data from the constant variables for each stacked entry from a row.

默认值是所有变量not specified byvars.。您可以指定'ConstantVariables'name-value pair argument to exclude variables not specified byvars.or'ConstantVariables'from the output table,S.

可以沿着它的第一层有行标签。如果is a table, then it can have row names as the labels. If是一个时间表,那么它必须将行时间与标签有关。

  • 您可以在指定值时包含行名称或行时'ConstantVariables'

  • 即使您不包含它们,即使在其中复制行名称或行时'ConstantVariables'

名称for the new data variable inS.那specified as the comma-separated pair consisting of'NewDataVariableName'和一个字符矢量或字符串标量。The default is a concatenation of the names of the variables from那是堆积的。

名称for the new indicator variable inS.那specified as the comma-separated pair consisting of'IndexVariableName'和一个字符矢量或字符串标量。默认值是基于的名称newdatavariablename.

Output Arguments

collapse all

堆叠的表,作为表或时间表返回。S.包含堆叠数据变量,分类指示器变量和任何常数变量。

You can store additional metadata such as descriptions, variable units, variable names, and row names inS.。有关更多信息,请参阅the Properties sections oftableor时间表

assigns the variable units and variable description property values from the first variable listed invars.to the correspondingS.。Properties.VariableUnitsands.properties.variabledescriscistions.values for the new data variable.

指数到那returned as a column vector. The index vector,iu那identifies the row in the input table,那containing the corresponding data.创造了j输出表中的行,S.那using你(iu(j),vars)

Tips

  • You can specify more than one group of data variables in那and each group becomes a stacked data variable inS.。你se a cell array to contain multiple values forvars.,以及字符向量或字符串阵列的单元阵列,用于包含多个值'NewDataVariableName'名称值对参数。所有组必须包含相同数量的变量。

扩展能力

Introduced in R2013b