-1

I have a data frame with 30 columns. My goal is to select 5 columns at a time. Based on a condition, I want to shift my selection in the data frame 5 columns.

For example:

Original dataframe column selection:

Col6 Col7 Col8 Col9 Col10

If condition is true, select:

Col1 Col2 Col3 Col4 Col5

If condition is false, select:

Col11 Col12 Col13 Col14 Col15.

I don't want to call out those specific columns though, I want to write a general formula that would call out a shift of 5 columns to the right or left based on whether the condition was true or false.

Any help would be great. Thanks!

Jaap
  • 81,064
  • 34
  • 182
  • 193
Nick
  • 833
  • 2
  • 8
  • 11
  • 1
    Please share example data. – theamateurdataanalyst Jul 13 '15 at 16:50
  • So, you are only selecting columns 1:5 and 11:15 based on the condition? – akrun Jul 13 '15 at 16:52
  • I can't but I basically just want to shift my selection a specific ncolumns to the right or left from my starting column selection point. – Nick Jul 13 '15 at 16:53
  • @akrun I'm not starting with those columns specifically, I just want to know if there is a function that can shift my column selection a number of columns to the left of right – Nick Jul 13 '15 at 16:54

1 Answers1

2

From what you asked I developed the following code. Please clarify the question if this is not what you are looking for. Assuming that your data frame is df:

n <- 1

df[,n:n+4]

if (condition){
  n <- n + 5
} else if (condition && n != 1) {
  n <- n - 5
} 
Paulo MiraMor
  • 1,582
  • 12
  • 30