如何使用“5k + 2”,“5k + 2和5k + 3”以及“5k + 2和5k + 3和5k + 4”图案条件对数据帧进行子集化?

时间:2016-08-08 02:13:11

标签: r dataframe subset

我有一个数据帧“mydata”,其中151列(变量)在开始列中保存了5个系统变量。

nx:= no. of independent variables    
ny:= no. of dependent variables    
nz:= no. of variables over which conditioning is performed. 

nx + ny + nz = 5(nx,ny,nz> 0)

mydata中变量的排序是常规的:
独立....家属.... conditioneds ... ...不变... 1stLagsOfIndependents ... 1stLagsOfDependents ... 1stLagsOfConditioneds ... 2ndLagsOfIndependents ... 2ndLagsOfDependents ........... 2ndLagsOfConditioneds ..... 29ndLagsOfIndependents ... 29ndLagsOfDependents ... 29ndLagsOfConditioneds

mydata的结构如下:
5系统变量常量1stLagsOfVariables 2ndLagsOfVariables ...... 29thLagsOfVariables
V1 V2 V3 V4 V5 .......... 1 ........ V11l V21l .... V51l V12l V22l .... V52l ............ .... V129l V229l .... V529l

索引结构是:

5.........1......5.........5.............................5   
(1-5)....(6)...(7-11)...(12-16)..................(5*29+2=147 - 151)

用户指定号码。开头的自变量:

nx <- 1   # or s/he may choose nx <- 2 etc. 

如何以简洁(编程)的方式为mydata子集指定类型的滞后?即,
对于nx = 1; 我需要简明扼要地表达这一点:

mydata[,c(7,12,...,147)] # V11l V12l ... V129l   
mydata[,c(5*k+2)] , k=1,...,29

对于nx = 2; 我需要简明扼要地表达这一点:

mydata[,c(7,8,12,13,...,147,148)]  # V11l V21l V12l V22l... V129l V229l  
mydata[,c(5*k+2, 5*k+3)] , k=1,...,29

对于nx = 3; 我需要简明扼要地表达这一点:

mydata[,c(7,8,9,12,13,14,...,147,148,149)] # V11l V21l V31l V12l V22l V32l ... V129l V229l V329l    
mydata[,c(5*k+2, 5*k+3, 5*k+4)] , k=1,...,29

nx不能为4(nx + ny + nz = 5且nx,ny,nz> 0)。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

尝试mydata[,(7:151)[rep(c(rep(TRUE, nx), rep(FALSE, 5-nx)), times=29)]]