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@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.