[R] aggregate combination data

Rui Barradas ruipbarradas at sapo.pt
Wed Nov 14 15:04:19 CET 2012


Hello,

Try the following.


fun <- function(x, k){
     n <- length(x)
     cmb <- combn(n, k)
     apply(cmb, 2, function(j) x[j])
}

uplot <- unique(dat$plot)
fun(uplot, 2)

Hope this helps,

Rui Barradas
Em 14-11-2012 13:42, catalin roibu escreveu:
> Hello again,
> I want the individual plots aggregated by k-combination. I have 100 plots.
>   For example I want all values of diameter (d) for combination of two plots
> (plot 1 and 2, plot 1 and 3 etc) taken by 100.
>
>
> On 14 November 2012 15:38, Rui Barradas <ruipbarradas at sapo.pt> wrote:
>
>> Hello,
>>
>> This will give you a list of 19 elements, each element k is a matrix of
>> all combinations of 19 taken k at a time.
>>
>> cmb <- lapply(seq_len(nrow(dat)), function(k) combn(nrow(dat), k))
>> d <- dat[["d"]]
>> lapply(cmb, function(cc) apply(cc, 2, function(j) d[j]))
>>
>>
>>
>> Hope this helps,
>>
>> Rui Barradas
>> Em 14-11-2012 13:32, catalin roibu escreveu:
>>
>>   I want to aggregate all values of diameter after the combination between
>>> plots. For example all diameter (d) values for combination of n plots
>>> taken
>>> by k.
>>>
>>>
>>> On 14 November 2012 15:28, Rui Barradas <ruipbarradas at sapo.pt> wrote:
>>>
>>>   Hello,
>>>> You forgot to Cc the list.
>>>> As for your question, you want all possible combinations of rows? For all
>>>> possible values of k in 1:19?
>>>>
>>>>   sum(sapply(1:19, function(k) choose(19, k)))
>>>> [1] 524287
>>>>
>>>> Or you want to split the data.frame by plot and the have all possible
>>>> combinations?
>>>>
>>>>   sum(sapply(1:10, function(k) choose(10, k)))
>>>> [1] 1023
>>>>
>>>>> sum(sapply(1:9, function(k) choose(9, k)))
>>>>>
>>>> [1] 511
>>>>
>>>> Rui Barradas
>>>> Em 14-11-2012 11:31, catalin roibu escreveu:
>>>>
>>>>    I don't want to sum the data from all combination. I want to group
>>>>
>>>>> (aggregate) the all values resulted from all combination possible.
>>>>> plot d
>>>>> 1 14
>>>>> 1 13
>>>>> 1 12
>>>>> 1 14
>>>>> 1 18
>>>>> 1 20
>>>>> 1 21
>>>>> 1 43
>>>>> 1 108
>>>>> 1 43
>>>>> 2 41
>>>>> 2 61
>>>>> 2 83
>>>>> 2 61
>>>>> 2 84
>>>>> 2 45
>>>>> 2 21
>>>>> 2 12
>>>>> 2 11
>>>>>
>>>>>
>>>>> On 14 November 2012 13:25, Rui Barradas <ruipbarradas at sapo.pt> wrote:
>>>>>
>>>>>    Hello,
>>>>>
>>>>>> Please use ?dput to post your data.
>>>>>>
>>>>>> dput(MyData)  # paste the output of this in a post.
>>>>>>
>>>>>> And you must be more clear, what does "aggregate" mean? To sum? In the
>>>>>> mean time see
>>>>>>
>>>>>> ?combn
>>>>>>
>>>>>> Hope this helps,
>>>>>>
>>>>>> Rui Barradas
>>>>>> Em 14-11-2012 11:11, catalin roibu escreveu:
>>>>>>
>>>>>>    Dear R users,
>>>>>>
>>>>>>> I want to aggregate all *d *data from  all combination of n *plots*
>>>>>>> taken
>>>>>>>
>>>>>>> by k.
>>>>>>> Thank very much!
>>>>>>>
>>>>>>> My data is like that:
>>>>>>>                plot              d  1 14  1 13  1 12  1 14  1 18  1 20
>>>>>>>   1
>>>>>>> 21
>>>>>>>     1
>>>>>>> 43  1 108  1 43  2 41  2 61  2 83  2 61  2 84  2 45  2 21  2 12  2 11
>>>>>>>     ...  100
>>>>>>>           10
>>>>>>> 100       12
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>




More information about the R-help mailing list