acgc.stats.tapply

tapply

 1#!/usr/bin/env python3
 2'''tapply'''
 3
 4import numpy as np
 5
 6__all__ = ['tapply']
 7
 8def tapply( array, group, f ):
 9    '''Apply user-specified function to array elements within each group
10
11    Reproduces tapply function in R
12    
13    Parameters
14    ----------
15    array : array_like
16        values that will be grouped
17    group : array_like
18        group ids for the elements of array, must have same size as array
19    f : callable
20        function with one argument that will be applied to each group, e.g. np.mean
21
22    Returns
23    -------
24    result : array_like
25        value of function applied to each group. 
26        The number of elements in result equals the number of unique elements of the group argument
27    groupvalues : array_like
28        group ids corresponding to the elements of result
29    '''
30
31    # Ensure that arguments have the same size
32    assert array.size == group.size, "Arguments array and group must have the same size"
33
34    # Find the unique GROUP values
35    groupvalues = np.unique( group )
36
37    # Make an array with the type as the input array
38    result = np.empty( groupvalues.size, type(array[0]) )
39
40    # Loop over the number of unique values
41    for i in range(groupvalues.size):
42
43        # Find which elements share the same value
44        index = np.where( group == groupvalues[i] )
45
46        # Apply teh given function to the common elements
47        result[i] = f(array[index])
48
49    # return
50    return result, groupvalues
def tapply(array, group, f):
 9def tapply( array, group, f ):
10    '''Apply user-specified function to array elements within each group
11
12    Reproduces tapply function in R
13    
14    Parameters
15    ----------
16    array : array_like
17        values that will be grouped
18    group : array_like
19        group ids for the elements of array, must have same size as array
20    f : callable
21        function with one argument that will be applied to each group, e.g. np.mean
22
23    Returns
24    -------
25    result : array_like
26        value of function applied to each group. 
27        The number of elements in result equals the number of unique elements of the group argument
28    groupvalues : array_like
29        group ids corresponding to the elements of result
30    '''
31
32    # Ensure that arguments have the same size
33    assert array.size == group.size, "Arguments array and group must have the same size"
34
35    # Find the unique GROUP values
36    groupvalues = np.unique( group )
37
38    # Make an array with the type as the input array
39    result = np.empty( groupvalues.size, type(array[0]) )
40
41    # Loop over the number of unique values
42    for i in range(groupvalues.size):
43
44        # Find which elements share the same value
45        index = np.where( group == groupvalues[i] )
46
47        # Apply teh given function to the common elements
48        result[i] = f(array[index])
49
50    # return
51    return result, groupvalues

Apply user-specified function to array elements within each group

Reproduces tapply function in R

Parameters
  • array (array_like): values that will be grouped
  • group (array_like): group ids for the elements of array, must have same size as array
  • f (callable): function with one argument that will be applied to each group, e.g. np.mean
Returns
  • result (array_like): value of function applied to each group. The number of elements in result equals the number of unique elements of the group argument
  • groupvalues (array_like): group ids corresponding to the elements of result