[R] indexing lists
David Winsemius
dwinsemius at comcast.net
Mon Nov 15 23:59:06 CET 2010
On Nov 15, 2010, at 5:07 PM, Chris Carleton wrote:
> Thanks for the suggestions. The issue for me is that the top level
> index is
> also like a database key so it might be a bit annoying to coerce it to
> char() so that I can reference it with a $ and then I would have to
> still be
> able to find out what the name was automatically. I've got a
> function right
> now that iterates through a list of values (db keys called cat
> values in
> this case) that returns an object from another function that will be
> used in
> yet another function. So, I have to store the objects and then pass
> each one
> in turn to a function while keeping track of what cat value that
> object is
> associated with so that it can be stored in relation to the cat
> value in a
> dataframe. Essentially like this...
>
> for i in cat {
Please do not use "cat" as an object name for the same reason as not
to use "c" or "data" as object names.
> object <- somefunction()
> alist[[ i ]] <- object
> }
>
> for i in cat {
> result <- somefunction(object[[ i ]])
> adataframe[[ i, c('cat','result') ]] <- c( i, result)
I don't think the [[ operator take two arguments. Perhaps you meant to
use the "[" operator. Even then cannot tell what "cat" is supposed to
be and quoteing cat would prevent it from being evaluated.
> }
>
> I'm paranoid about loosing track of which cat value is associated
> with which
> result and that's why I'm looking for a way to ensure that the
> output is
> stored correctly. The whole thing is going to be automated. Any
> suggestions
> would definitely be appreciated. I've tried just creating a list of
> lists to
> keep track of things so that list[[1]][[1]] is the cat value and
> list[[1]][[2]] is the associated object, but now I'm having trouble
> passing
> the object to the next function. This might take some time for me to
> work
> out.
It appears you are too busy to make a working example, so I am too
busy to do it for you.
?names # for naming and access to names of list elements
--
David.
>
>
> Thanks,
>
> Chris
>
>
> On 15 November 2010 16:38, Joshua Wiley <jwiley.psych at gmail.com>
> wrote:
>
>> Hi Chris,
>>
>> Does this do what you're after? It just compares each element of a
>> (i.e., a[[1]] and a[[2]]) to c(1, 2) and determines if they are
>> identical or not.
>>
>> which(sapply(a, identical, y = c(1, 2)))
>>
>> There were too many 1s floating around for me to figure out if you
>> wanted to find elements of a that matched the entire vector or
>> subelements of a that matched elements of the vector (if that makes
>> any sense).
>>
>> HTH,
>>
>> Josh
>>
>> On Mon, Nov 15, 2010 at 1:24 PM, Chris Carleton
>> <w_chris_carleton at hotmail.com> wrote:
>>> Hi List,
>>>
>>> I'm trying to work out how to use which(), or another function, to
>>> find
>> the
>>> top-level index of a list item based on a condition. An example will
>> clarify
>>> my question.
>>>
>>> a <- list(c(1,2),c(3,4))
>>> a
>>> [[1]]
>>> [1] 1 2
>>>
>>> [[2]]
>>> [1] 3 4
>>>
>>> I want to find the top level index of c(1,2), which should return 1
>> since;
>>>
>>> a[[1]]
>>> [1] 1 2
>>>
>>> I can't seem to work out the syntax. I've tried;
>>>
>>> which(a == c(1,2))
>>>
>>> and an error about coercing to double is returned. I can find the
>>> index
>> of
>>> elements of a particular item by
>>>
>>> which(a[[1]]==c(1,2)) or which(a[[1]]==1) etc that return [1] 1 2
>>> and [1]
>> 1
>>> respectively as they should. Any thoughts?
>>>
>>> C
>>>
>>> [[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.
>>>
>>
>>
>>
>> --
>> Joshua Wiley
>> Ph.D. Student, Health Psychology
>> University of California, Los Angeles
>> http://www.joshuawiley.com/
>>
>>
>
> [[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.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list