[R] Converting list to character

Massimiliano Tripoli mtripoli at istat.it
Tue Nov 25 13:07:38 CET 2014



Dear all,

I can't convert the result of aggregate function in a dataframe. My data
looks like:

mydata <- structure(list(ID = c(11, 11, 460, 460, 986, 986, 986, 986, 1251,
1251, 1251, 1251, 1251, 1251, 1251, 1251, 1801, 1801, 1801, 1801
), YEAR = c(2009, 2010, 2010, 2011, 2008, 2009, 2010, 2011, 2008,
2008, 2009, 2009, 2010, 2010, 2011, 2011, 2008, 2009, 2010, 2011
), Y = c(158126, 153015, 3701, 5880, 718663, 661112, 527233,
558281, 450, 131714, 427, 124648, 425, 116500, 434, 123853, 17400,
16493, 8057, 8329), CODE = c("GR.3.7", "GR.3.7", "GR.3.1", "GR.3.1",
"GR.3.8", "GR.3.8", "GR.3.8", "GR.3.8", "GR.3.1", "GR.3.8", "GR.3.1",
"GR.3.8", "GR.3.1", "GR.3.8", "GR.3.1", "GR.3.8", "GR.3.8", "GR.3.8",
"GR.3.8", "GR.3.8")), .Names = c("ID", "YEAR", "Y", "CODE"), row.names = c(NA,
20L), class = "data.frame")

and by using aggregate function

TAB <- aggregate(mydata$CODE,by=list(ID=mydata$ID,YEAR=mydata$YEAR),FUN=paste0)

What I want is a dataframe like of printing TAB:
> TAB
     ID YEAR              x
1   986 2008         GR.3.8
2  1251 2008 GR.3.1, GR.3.8
3  1801 2008         GR.3.8
4    11 2009         GR.3.7
5   986 2009         GR.3.8
6  1251 2009 GR.3.1, GR.3.8
7  1801 2009         GR.3.8
8    11 2010         GR.3.7
9   460 2010         GR.3.1
10  986 2010         GR.3.8
11 1251 2010 GR.3.1, GR.3.8
12 1801 2010         GR.3.8
13  460 2011         GR.3.1
14  986 2011         GR.3.8
15 1251 2011 GR.3.1, GR.3.8
16 1801 2011         GR.3.8

> str(TAB)[1:10]
'data.frame':        16 obs. of  3 variables:
 $ ID  : num  986 1251 1801 11 986 ...
 $ YEAR: num  2008 2008 2008 2009 2009 ...
 $ x   :List of 16
  ..$ 1 : chr "GR.3.8"
  ..$ 2 : chr  "GR.3.1" "GR.3.8"
  ..$ 4 : chr "GR.3.8"
  ..$ 5 : chr "GR.3.7"
  ..$ 6 : chr "GR.3.8"
  ..$ 7 : chr  "GR.3.1" "GR.3.8"
  ..$ 9 : chr "GR.3.8"
  ..$ 10: chr "GR.3.7"
  ..$ 11: chr "GR.3.1"
  ..$ 12: chr "GR.3.8"
  ..$ 13: chr  "GR.3.1" "GR.3.8"
  ..$ 15: chr "GR.3.8"
  ..$ 16: chr "GR.3.1"
  ..$ 17: chr "GR.3.8"
  ..$ 18: chr  "GR.3.1" "GR.3.8"
  ..$ 20: chr "GR.3.8"
NULL

As you can see the "x" coloumn is a list and I would want to change it to character variable.
Anyone may help me?
Thanks,

Massimiliano
-- 
Massimiliano Tripoli 
Collaboratore T.E.R. scado il 31/12/2014 
ISTAT - DCCN - Direzione Centrale della Contabilità Nazionale 
U.O. Contabilità dei flussi di materia del sistema economico - CSA/C 
Via Depretis, 74/B 00184 Roma 
Tel. 06.4673.3132 
E-mail: mtripoli at istat.it 



More information about the R-help mailing list