[R] variable length lisin in data frame
William Dunlap
wdunlap at tibco.com
Sat May 10 17:50:23 CEST 2014
> d <- data.frame(id=1:4, no.contacts=c(2,3,3,1))
> d$contacts_list <- list(3:4, c(1,3,4), c(4,2,1), 1 )
If you store that information in a longer format, with each row being
an edge to the relationship graph, it can make further processing
easier:
d2 <- with(d,
data.frame(id=rep(id, vapply(contacts_list,length,0)),
contacts_list=unlist(contacts_list)))
At that point you may want to install and load the igraph package,
convert d2 to an 'igraph' object, and use use package:igraph's
functions on it:
install.packages("igraph")
library(igraph)
g <- graph.data.frame(d2)
neighbors(g, 2, mode="out")
# [1] 1 3 4
neighbors(g, 2, mode="in")
# [1] 3
plot(g)
Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Sat, May 10, 2014 at 5:20 AM, Duncan Murdoch
<murdoch.duncan at gmail.com> wrote:
> On 10/05/2014, 7:46 AM, Ragia Ibrahim wrote:
>>
>> Dear Group,
>> I have data like the following
>>
>> id contacts_list number of contacts
>> ---------------------------------------------------
>> 1 3 4 2
>> 2 1 3 4 3
>> 3 4 2 1 3
>> 4 1 1
>> ------------------------------------------------------
>>
>>
>> can you kindly please sugest a data type in R to use for it?
>> i thought about data frame that consists of a list element. but lists
>> are not in the same length, any solutions?
>
>
> You can use a dataframe, but the data.frame function gets confused by the
> contacts_list, so you need to set it up in two steps, e.g.
>
> d <- data.frame(id=1:4, no.contacts=c(2,3,3,1))
> d$contacts_list <- list(3:4, c(1,3,4), c(4,2,1), 1 )
>
> Other dataframe functions (e.g. printing, indexing, etc.) should work:
>
>> d
> id no.contacts contacts_list
> 1 1 2 3, 4
> 2 2 3 1, 3, 4
> 3 3 3 4, 2, 1
> 4 4 1 1
>
>> d[2,]
> id no.contacts contacts_list
> 2 2 3 1, 3, 4
>
> A slightly confusing bit is that if you pick out a single element of column
> 3 you'll get a list containing it:
>
>> d[2,3]
> [[1]]
> [1] 1 3 4
>
> so you should use the double bracket list-indexing style:
>
>> d[[2,3]]
> [1] 1 3 4
>
> Duncan Murdoch
>
>
> ______________________________________________
> 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.
More information about the R-help
mailing list