Re: [R] aggregate and list elements of variables in data.frame

2018-06-08 Thread Eik Vettorazzi
Hi, if you are willing to use dplyr, you can do all in one line of code: library(dplyr) df<-data.frame(id=1:10,A=c(123,345,123,678,345,123,789,345,123,789)) df%>%group_by(unique_A=A)%>%summarise(list_id=paste(id,collapse=", "))->r cheers Am 06.06.2018 um 10:13 schrieb Massimo Bressan: > #given

Re: [R] aggregate and list elements of variables in data.frame

2018-06-07 Thread Bert Gunter
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

Re: [R] aggregate and list elements of variables in data.frame

2018-06-07 Thread Massimo Bressan
#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<-dat

Re: [R] aggregate and list elements of variables in data.frame

2018-06-07 Thread Massimo Bressan
vals<- lapply(idx, function(index) x$id[index]) data.frame(unique_A = uA, list_vals=unlist(lapply(vals, paste, collapse = ", "))) best Da: "Ben Tupper" A: "Massimo Bressan" Cc: "r-help" Inviato: Giovedì, 7 giugno 2018 14:47:55 Oggetto: Re: [

Re: [R] aggregate and list elements of variables in data.frame

2018-06-07 Thread Ben Tupper
Hi, Does this do what you want? I had to change the id values to something more obvious. It uses tibbles which allow each variable to be a list. library(tibble) library(dplyr) x <- tibble(id=LETTERS[1:10], A=c(123,345,123,678,345,123,789,345,123,789)) uA <- unique(x$

Re: [R] aggregate and list elements of variables in data.frame

2018-06-07 Thread Ivan Calandra
Using which() to subset t$id should do the trick: sapply(levels(t$A), function(x) t$id[which(t$A==x)]) Ivan -- Dr. Ivan Calandra TraCEr, laboratory for Traceology and Controlled Experiments MONREPOS Archaeological Research Centre and Museum for Human Behavioural Evolution Schloss Monrepos 56567

Re: [R] aggregate and list elements of variables in data.frame

2018-06-07 Thread Massimo Bressan
sorry, but by further looking at the example I just realised that the posted solution it's not completely what I need because in fact I do not need to get back the 'indices' but instead the corrisponding values of column A #please consider this new example t<-data.frame(id=c(18,91,20,68,54,27

Re: [R] aggregate and list elements of variables in data.frame

2018-06-07 Thread Massimo Bressan
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)

Re: [R] aggregate and list elements of variables in data.frame

2018-06-06 Thread Ivan Calandra
Hi Massimo, Something along those lines could help you I guess: t$A <- factor(t$A) sapply(levels(t$A), function(x) which(t$A==x)) You can then play with the output using paste() Ivan -- Dr. Ivan Calandra TraCEr, laboratory for Traceology and Controlled Experiments MONREPOS Archaeological Resea

[R] aggregate and list elements of variables in data.frame

2018-06-06 Thread Massimo Bressan
#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