dapply                  package:emu                  R Documentation

_a_p_p_l_y _a _f_u_n_c_t_i_o_n _t_o _e_a_c_h _p_a_r_t _o_f _a _t_r_a_c_k_d_a_t_a _o_b_j_e_c_t

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

     Given an Emu trackdata object, 'dapply' will apply a given 
     function to the data corresponding to each segment of data. The 
     result is a new trackdata object.

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

     dapply(trackdata, fun, ...)

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

trackdata: An Emu trackdata object 

     fun: A function taking a matrix of data and a vector of times and
          returning a list with components '\$data' and '\$ftime'.

     ...: Additional arguments to be passed to 'fun' 

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

     'dapply' can be used to apply an arbitrary function to trackdata
     extracted from an Emu database. It can be used for example to
     smooth the data (see 'dsmooth') or differentiate it (see 'ddiff').

     Trackdata is made up of three components: a matrix of data
     '\$data', a matrix of indexes ('\$index') and a matrix of segment
     times ('\$ftime').  The indexes contain the start and end rows for
     each segment in the trackdata, the time matrix contains the start
     and end times of each data segment.

     The function 'fun' supplied to 'dapply' should take one matrix of
     data (corresponding to one segment) and a vector of two times
     being the start and end of the data.  It should return a modified
     data matrix, which can have any number of rows or columns, and a
     new pair of start and end times.  The new start and end times are
     necessary because the operation applied might shorten or
     interpolate the data and hence change the times corresponding to
     the first and last rows of data.

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

     An Emu trackdata object with components: 

    data: A matrix of data with all segments concatenated by row.

   index: A two column matrix of the start and end rows for each
          segment

   ftime: A two column matrix of the start and end times for each
          segment

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

     'dsmooth' 'ddiff'

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

     data(dip)
     ## formant data of the first segment in segment list dip
     fm <- dip.fdat[1]

     testfun <- function(data, ftime, n) {
       ## return only the first n rows of data
       ## doesn't check to see if there really are n rows...
       newdata <- data[1:n,]
       ## calculate a new end time
       interval <- (ftime[2]-ftime[1])/nrow(data)
       ftime[2] <- ftime[1] + interval*n
       ## now return the required list 
       return( list( data=newdata, ftime=ftime ) )
     }

     fm.first3 <- dapply( fm, testfun, 3 )
     fm.first10 <- dapply( fm, testfun, 10 )

