[R] iterative extracting data from a list without keys
Bert Gunter
gunter.berton at gene.com
Thu Mar 7 22:18:56 CET 2013
Re-read
?"["
(always better to read the docs before archives...)
and note in particular:
"[[ can be applied recursively to lists, so that if the single index i
is a vector of length p, alist[[i]] is equivalent to
alist[[i1]]...[[ip]] providing all but the final indexing results in a
list."
I agree -- it's terse. But it's accurate and complete.
-- Bert
On Thu, Mar 7, 2013 at 12:14 PM, Jeff Hollenbeck <hollenb at peak.org> wrote:
> 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 at 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.
--
Bert Gunter
Genentech Nonclinical Biostatistics
Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
More information about the R-help
mailing list