Erdogan CEVHER - 1 year ago 76
R Question

How to subset a dataframe with "5k+2", "5k+2 and 5k+3", and "5k+2 and 5k+3 and 5k+4" patterned-condition?

I have a dataframe "mydata" with 151 columns (variables) holding 5-system variables at the beginning columns.

``````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)

The ordering of variables in mydata is regular:

independents....dependents....conditioneds...constant...1stLagsOfIndependents...1stLagsOfDependents...1stLagsOfConditioneds...2ndLagsOfIndependents...2ndLagsOfDependents...2ndLagsOfConditioneds................29ndLagsOfIndependents...29ndLagsOfDependents...29ndLagsOfConditioneds

The mydata's structure is as follows:

5-system-variables constant 1stLagsOfVariables 2ndLagsOfVariables......29thLagsOfVariables

V1 V2 V3 V4 V5 ..........1........ V11l V21l....V51l V12l V22l....V52l................V129l V229l....V529l

The index structure is:

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

User specifies no. of independent variables at the beginning:

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

How to subset mydata in a concise (programmatic) way for lags of specified type? i.e,

For nx=1;
I need to express concisely this one:

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

For nx=2;
I need to express concisely this one:

``````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
``````

For nx=3;
I need to express concisely this one:

``````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 cannot be 4 since (nx+ny+nz=5 and nx,ny,nz>0).

Any help will be greatly appreciated.

Try `mydata[,(7:151)[rep(c(rep(TRUE, nx), rep(FALSE, 5-nx)), times=29)]]`.