thanks for the help I'm posting here the complete solution
t<-data.frame(id=1:10,A=c(123,345,123,678,345,123,789,345,123,789)) t$A <- factor(t$A) l<-sapply(levels(t$A), function(x) which(t$A==x)) r<-data.frame(list_id=unlist(lapply(l, paste, collapse = ", "))) r<-cbind(unique_A=row.names(r),r) row.names(r)<-NULL r best Da: "Massimo Bressan" <massimo.bres...@arpa.veneto.it> A: "r-help" <R-help@r-project.org> Inviato: Mercoledì, 6 giugno 2018 10:13:10 Oggetto: aggregate and list elements of variables in data.frame #given the following reproducible and simplified example t<-data.frame(id=1:10,A=c(123,345,123,678,345,123,789,345,123,789)) t #I need to get the following result r<-data.frame(unique_A=c(123, 345, 678, 789),list_id=c('1,3,6,9','2,5,8','4','7,10')) r # i.e. aggregate over the variable "A" and list all elements of the variable "id" satisfying the criteria of having the same corrisponding value of "A" #any help for that? #so far I've just managed to "aggregate" and "count", like: library(sqldf) sqldf('select count(*) as count_id, A as unique_A from t group by A') library(dplyr) t%>%group_by(unique_A=A) %>% summarise(count_id = n()) # thank you -- ------------------------------------------------------------ Massimo Bressan ARPAV Agenzia Regionale per la Prevenzione e Protezione Ambientale del Veneto Dipartimento Provinciale di Treviso Via Santa Barbara, 5/a 31100 Treviso, Italy tel: +39 0422 558545 fax: +39 0422 558516 e-mail: massimo.bres...@arpa.veneto.it ------------------------------------------------------------ [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.