# Graph\_from\_data\_frame

**URL:** <https://igraph.discourse.group/t/graph-from-data-frame/284>\
**Category:** Usage\
**Tags:** R\
**Created:** [9 June 2020 09:24 UTC](https://igraph.discourse.group/t/graph-from-data-frame/284 "2020-06-09T09:24:43Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rcowan](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@rcowan](https://igraph.discourse.group/u/rcowan)\
**Post date:** [9 June 2020 09:24 UTC](https://igraph.discourse.group/t/graph-from-data-frame/284/1 "2020-06-09T09:24:43Z")

</div>

I am creating a graph from a data frame which contains factors. `graph_from_data_frame()` converts them to characters with these lines: (I saw on an old post by Gábor on StackOverflow)

```auto
newval <- d[, i]
if (class(newval) == "factor") {
  newval <- as.character(newval)
}
attrs[[names(d)[i]]] <- newval

```

So I wrote a new function `gfdf` with these lines commented out (following Gábor’s suggestion)

```auto
newval <- d[, i]
#if (class(newval) == "factor") {
 #newval <- as.character(newval)
#}
attrs[[names(d)[i]]] <- newval

```

But the conversion still seems to be happening.

```auto
> g=gfdf(zEdges,vertices=zNodes)
> is.factor(zNodes$CLEAN.GENDER)
[1] TRUE
> is.factor(V(g)$CLEAN.GENDER)
[1] FALSE

```

But curiously:

```auto
> head(zNodes$CLEAN.GENDER)
[1] H H H H H H
Levels: H M x
> head(V(g)$CLEAN.GENDER)
[1] 2 2 2 2 2 2

```

`V(g)$CLEAN.GENDER` has become numeric

It would be very convenient to keep these things as factors.  
What am I missing?

`igraph.version()` is `1.2.4`

Thanks  
Robin Cowan
