[R] Converting list to character

Massimiliano Tripoli mtripoli at istat.it
Wed Nov 26 11:27:47 CET 2014


Thanks David,
that's I was looking for.
Thanks to Chel too.

Massimiliano

----- Messaggio originale -----
Da: "David L Carlson" <dcarlson at tamu.edu>
A: "Chel Hee Lee" <chl948 at mail.usask.ca>, "Massimiliano Tripoli" <mtripoli at istat.it>, r-help at r-project.org
Inviato: Martedì, 25 novembre 2014 19:40:51
Oggetto: RE: [R] Converting list to character

Or just modify your aggregate() command:

> TAB <- aggregate(mydata$CODE, by=list(ID=mydata$ID, 
+        YEAR=mydata$YEAR), FUN=paste0, collapse=", ")
> 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

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352



-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Lee, Chel Hee
Sent: Tuesday, November 25, 2014 11:23 AM
To: Massimiliano Tripoli; r-help at r-project.org
Subject: Re: [R] Converting list to character

 > do.call("rbind", TAB$x)
    [,1]     [,2]
1  "GR.3.8" "GR.3.8"
2  "GR.3.1" "GR.3.8"
4  "GR.3.8" "GR.3.8"
5  "GR.3.7" "GR.3.7"
6  "GR.3.8" "GR.3.8"
7  "GR.3.1" "GR.3.8"
9  "GR.3.8" "GR.3.8"
10 "GR.3.7" "GR.3.7"
11 "GR.3.1" "GR.3.1"
12 "GR.3.8" "GR.3.8"
13 "GR.3.1" "GR.3.8"
15 "GR.3.8" "GR.3.8"
16 "GR.3.1" "GR.3.1"
17 "GR.3.8" "GR.3.8"
18 "GR.3.1" "GR.3.8"
20 "GR.3.8" "GR.3.8"
 >

Is this what you are looking for?  I hope this helps.

Chel Hee Lee

On 11/25/2014 6:07 AM, Massimiliano Tripoli wrote:
>
>
> 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
>

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

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