views:

137

answers:

1

I have the following dataframe (df)

 start     end
1    14379   32094
2   151884  174367
3   438422  449382
4   618123  621256
5   698271  714321
6   973394  975857
7   980508  982372
8   994539  994661
9  1055151 1058824
.   .       .
.   .       .
.   .       .

And a long vector with numeric values (vec).

I would like to add to each row another column, with the mean of the values in the corresponding places in vec. for example, the first row will have mean(vec[14379:32094]). I have tried playing with transform but wasn't able to accomplish this simple task.

+4  A: 

That is a pretty standard use case for apply():

R> vec <- 1:10
R> DF <- data.frame(start=c(1,3,5,7), end=c(2,6,7,9))
R> DF$newcol <- apply(DF,1,function(row) mean(vec[ row[1] : row[2] ] ))
R> DF
  start end newcol
1     1   2    1.5
2     3   6    4.5
3     5   7    6.0
4     7   9    8.0
R> 

You can also use plyr if you prefer but here is no real need to go beyond functions from base R.

Dirk Eddelbuettel
+1 Thanks! Re. `plyr` - how can I use it if I want each row to be treated by itself?
David B
What are you talking about? This treats each by itself. How else would it do this?
Dirk Eddelbuettel
@Dirk Eddelbuettel each what? as far as I understand, `plyr` work on group of rows.
David B