igraph-help
[Top][All Lists]

## Re: [igraph] Neighors with apply

 From: Gábor Csárdi Subject: Re: [igraph] Neighors with apply Date: Thu, 9 Feb 2012 08:52:15 -0500

```On Thu, Feb 9, 2012 at 5:11 AM, Thomas <address@hidden> wrote:
> Gabor - thank you again for all your help! I ran:
>
> xt <- lapply(na.omit(dlist), neighbors, graph=g)
>
> and it produced:
>
> [[1]]
>  [1] 177 178 179 180 181 182 183 184 185 186
>
> [[2]]
>  [1] 82 83 84 85 86 87 88 89 90 91
>
> but I can't figure out how to access individual elements in each list. How
> do I get at the '180' for example? I tried:

The printout gives you a hint:
xt[[1]] is the 1st element of the list, a vector. xt[[1]][4] is the
4th element of this vector. In general, lists are indexed with double
brackets, unless you want to create a sublist, which is single
brackets.

G.

> xt[1,4]
>
> but that didn't work.
>
> I know this is a basic question about lists and I'm sorry to be so useless
> but I couldn't find the answer online or in Gentleman's book.
>
> Thomas
>
>
> On 8 Feb 2012, at 14:17, Gábor Csárdi wrote:
>
>> On Wed, Feb 8, 2012 at 8:45 AM, Thomas <address@hidden> wrote:
>>>
>>> I have a vector with a list of nodes, which does have NAs in it although
>>> they are always at the end of the list, and I'd like to get a list of all
>>> the neighbors of those nodes:
>>>
>>> dlist <- c(100, 101, 105, NA, NA)
>>>
>>> neighbors(g, 100)
>>> neighbors(g, 101)
>>> neighbors(g, 105)
>>>
>>> is there any way to do this using an apply function? Something like:
>>>
>>> sapply(dlist, function(neighbors) neighbors(g,))
>>> or
>>> lapply(dlist, neighbors, g)
>>
>>
>> You can do something like this:
>>
>> g <- graph.ring(10)
>> dlist <- c(1,2,3,NA,NA)
>>
>> lapply(na.omit(dlist), neighbors, graph=g)
>> lapply(dlist, function(v) if (is.na(v)) NA else neighbors(g, v))
>>
>> The first version drops the NAs before the neighbors() calls, the
>> second results NAs for them.
>>
>> Best,
>> Gabor
>>
>>>
>>> Thank you!
>>>
>>> Thomas
>>>
>>> _______________________________________________
>>> igraph-help mailing list
>>> https://lists.nongnu.org/mailman/listinfo/igraph-help
>>
>>
>>
>>
>> --
>> Gabor Csardi <address@hidden>     MTA KFKI RMKI
>
>

--
Gabor Csardi <address@hidden>     MTA KFKI RMKI

```