trapply                 package:emu                 R Documentation

_A _m_e_t_h_o_d _o_f _t_h_e _g_e_n_e_r_i_c _f_u_n_c_t_i_o_n _b_y _f_o_r _o_b_j_e_c_t_s _o_f _c_l_a_s_s '_t_r_a_c_k_d_a_t_a'

_D_e_s_c_r_i_p_t_i_o_n:

     A given function 'FUN' is applied to the data corresponding to
     each segment of data.

_U_s_a_g_e:

       trapply (trackdata, fun, ..., simplify = FALSE, returntrack = FALSE)

_A_r_g_u_m_e_n_t_s:

trackdata: a track data object 

     fun: a function that is applied to each segment

     ...: arguments of the function fun

simplify: simplify = TRUE , output is a matrix; simplify = FALSE a list
          is returned

returntrack: returntrack = FALSE , return a trackdata object

_D_e_t_a_i_l_s:

     trapply() applies a function iteratively to each segment of a
     trackdata object without the need for using a for-loop. It can be
     used to calculate, for example, the mean value of the data values
     of each segment separately. Any function that can be applied
     sensibly to trackdata[j]$data where j is a segment number can be
     used as the fun argument to trapply(). It is also possible to
     write your own function and use trapply() to apply it separately
     to each segment. Care needs to be taken in using trapply() in the
     following two ways. Firstly, the argument simplify=T should only
     be set if it can be guaranteed that  a vector of the same length
     or matrix of the same number of rows as the number of segments in
     the trackdata object is returned. For example, simplify=T can be
     used in calculating the mean per segment of a trackdata object,
     because there will only be one value (the mean) per segment.
     However, simplify should be set to F in calculating the range
     because here two values are returned per segment. Similarly use
     simplify=F n smoothing the data in which the number of values
     returned per segment is different.  Secondly, trapply() only
     applies a function to a single parameter; the function can be used
     to apply to a function to multi-parameter trackdata such as F1-F4,
     but then the function needs to be put inside apply() - see
     examples below.

_V_a_l_u_e:

     list or vector or matrix

_A_u_t_h_o_r(_s):

     Jonathan Harrington

_S_e_e _A_l_s_o:

     'apply'

_E_x_a_m_p_l_e_s:

     # mean f0 one value per segment
     m = trapply(vowlax.fund, mean, simplify=TRUE)
     # mean F1 - F4
     m = trapply(vowlax.fdat, apply, 2, mean, simplify=TRUE)
     # make a logical vector of any segments that have an F1 value
     # between their start time and end time greater than n Hz
     pfun <- function(x, n=1000) any(x > n)
     # greater than 1100 Hz
     temp = trapply(vowlax.fdat[,1], pfun, 1100, simplify=TRUE)
     # get the F2-range per segment
     r = trapply(vowlax.fdat[,2], range)
     # F2-range of 20th segment
     r[[20]]
     # DCT-smooth F2 with 10 coeffs
     # get the first 4 DCT coefficients
     f2.dct = trapply(vowlax.fdat[,2], dct, 3, simplify=TRUE)
     # dct-smooth F2 with the first 5 DCT coeffs
     f2sm = trapply(vowlax.fdat[,2], dct, 4, TRUE,  returntrack=TRUE)
     # Make new F2 trackdata such that each segment has
     # F2 divided by its F2 range
     pfun <- function(x) x/(diff(abs(range(x))))
     newf2 = trapply(vowlax.fdat[,2], pfun, returntrack=TRUE)

