igraph-help
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [igraph] Coercing vertex/edge attributes to new types


From: Gábor Csárdi
Subject: Re: [igraph] Coercing vertex/edge attributes to new types
Date: Thu, 14 May 2009 18:23:24 +0200

Alex, this is basically a side effect of how R converts between
character and numeric types, if you do assignment to a subset. E.g.
see

> ch <- character(10)
> ch[1:10] <- as.numeric(ch)
> class(ch)
[1] "character"

stays character, but

> num <- numeric(10)
> num[1:10] <- as.character(ch)
> class(num)
[1] "character"

is converted to character. And of course igraph uses the same
subscripted form, even you want to assign to all vertices/edges at the
same time.

We could work around this easily of course, I started a bug report on this:
https://bugs.launchpad.net/igraph/+bug/376556

Thanks,
Gabor

On Thu, May 14, 2009 at 6:08 PM, Alex D'Amour <address@hidden> wrote:
> All,
>
> I'm having a problem with a network where edges have an "appyear"
> attribute, which is a four-digit number representing a year.
> Unfortunately, I imported the appyear as text, and would like to
> change it to a numeric attribute.
>
> I would expect that the easiest way to do this would be:
>
> E(g)$appyear <- as.numeric(E(g)$appyear)
>
> But this doesn't change the attribute -- they remain character strings.
>
> I also tried, the following, but it caused no change:
>
> class(E(g)$appyear) <- "numeric"
> class(E(g)$appyear)
> [1] "character"
>
> The only way that I could make this type change happen was break the
> abstraction barrier and delve into the igraph object's list structure:
>
> g[[9]][[4]][["appyear"]] <- as.numeric(g[[9]][[4]][["appyear"]])
>
> or
>
> class(g[[9]][[4]][["appyear"]]) <- "numeric"
>
> Interestingly, converting from numeric to character works as expected
> using the standard igraph interface:
>
> E(g)$appyear <- as.character(E(g)$appyear)
>
> or
>
> class(E(g)$appyear) <- "character"
>
> Both of these generate character strings.
>
> Is there a reason that coercion only works in one direction in this
> case via the standard igraph interface? I understand that there are a
> million different ways to do this, including storing the attribute in
> a vector, converting that, then dropping and adding the appyear
> attribute, but this seems like the kind of thing you _should_ be able
> to do via igraph's interface.
>
> Thanks,
> Alex
>
>
> _______________________________________________
> igraph-help mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/igraph-help
>



-- 
Gabor Csardi <address@hidden>     UNIL DGM




reply via email to

[Prev in Thread] Current Thread [Next in Thread]