[R] variable length lisin in data frame

Duncan Murdoch murdoch.duncan at gmail.com
Sat May 10 14:20:06 CEST 2014


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



More information about the R-help mailing list