Dear R Users.

This seems like a simple task, but I'm stuck.

I have a list with 3 elements: (2 vectors and 1 matrix).  I wish to extract 
each of these data elements using index subscripts and multiply them with a 
vector multiplier.

What I have:
> betas
[1] 0.01 0.01 0.01

> LData[1]
$int
  [1] 1 1 1 1 1 1 1 1 1 1
$date
       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] 
  [1,]  152  153  154  155  156  157  158  159  160  161   
  [2,]  163  164  165  166  167  168  162  163  164  165
$land
  [1] 0 0 0 0 1 0 1 1 0 0 

What I'm trying to do:
I would like to have the elements in the vector, betas[1:3] multiplied by 
corresponding data elements in LData[[1:3]].
This is how I initially try to do it, but does not work:

> probs <- betas[1:3] * LData[[1:3]]
Error in LData[[1:3]] : recursive indexing failed at level 2

Apparently, the above is not allowed.  But, if I try

> probs <- betas[1:3] * LData[1:3]
Error in betas[1:3] * LData[1:3] : 
  non-numeric argument to binary operator

I can see that the list keys get in the way of the multiplication operation.

There just has to be a way to do this.  I have searched archives and find 
plenty of examples of extracting data elements individually, and even 
extraction of multiple vector elements (unfortunately one of mine is an 
matrix), but not my situation.  In particular, no examples attempting to 
vectorize the math operation.  yapply and mapply don't seem to be what I want, 
but what I've read of them confuses me.  Want to avoid for loops, unless its 
the only way.

All help is much appreciated!

Thanks.

*******************************************************************
Jeff P. Hollenbeck
USGS Forest and Rangeland Ecosystem Science Center
3200 SW Jefferson Way
Corvallis, OR 97331
541-750-0966

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to