which() is unnecessary. Use logical subscripting: ... t$id[t$A ==x]
Further simplification can be gotten by using the with() function: l <- with(t, sapply(unique(A), function(x) id[A ==x])) Check this though -- there might be scoping issues. Cheers, Bert On Thu, Jun 7, 2018, 6:49 AM Massimo Bressan <massimo.bres...@arpa.veneto.it> wrote: > #ok, finally this is my final "best and more compact" solution of the > problem by merging different contributions (thanks to all indeed) > > t<-data.frame(id=c(18,91,20,68,54,27,26,15,4,97),A=c(123,345,123,678,345,123,789,345,123,789)) > > l<-sapply(unique(t$A), function(x) t$id[which(t$A==x)]) > r<-data.frame(unique_A= unique(t$A), list_id=unlist(lapply(l, paste, > collapse = ", "))) > r > > > [[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. > [[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.