1

This question is about selecting a different number of columns on every row of a data frame. I have a data frame:

df = data.frame(
    START=sample(1:2, 10, repace=T), END=sample(2:4, 10, replace=T),
    X1=rnorm(10), X2=rnorm(10), X3=rnorm(10), X4=rnorm(10)
)

I would like to have a way without loops to select columns (START[i]:END[i])+2 on row i for all rows of my data frame.

josliber
  • 43,891
  • 12
  • 98
  • 133

1 Answers1

0

Base R solution

lapply(split(df,1:nrow(df)),function(row) row[(row$START+2):(row$END+2)])

Or something similar as given in the comment above (I would store the output in a list)

library(plyr)
alply(df,1,function(row) row[(row$START+2):(row$END+2)])

Edit per request of OP:

To get a TRUE/FALSE index matrix, use the following R base solution

idx_matrix=col(df)>=df$START+2&col(df)<=df$END+2
df[idx_matrix]

Note, however, that you lose some information here (compared to the list based solution).

cryo111
  • 4,444
  • 1
  • 15
  • 37
  • To be more clear by select I ment the array locations. Ideally in some sort of array of Trues and Falses. MY goal is to add the ith element of a list to each selected element in the ith row. – user2998362 Nov 18 '13 at 20:15
  • I have edited the answer above. This gives you a TRUE/FALSE index matrix. – cryo111 Nov 18 '13 at 21:54
  • This works great, thanks! So it turns out I was really asking for the functions col and row! Good to know – user2998362 Nov 18 '13 at 22:09
  • You are welcome (since you seem to be new to Stackoverflow: you can accept answers by checking the tick below the up/down voting arrows) – cryo111 Nov 18 '13 at 22:21