<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def remove_rm_na(dv=None, within=None, subject=None, data=None, aggregate='mean'):
 """Remove missing values in long-format repeated-measures dataframe. Parameters dv : string or list Dependent variable(s), from which the missing values should be removed. If ``dv`` is not specified, all the columns in the dataframe are considered. ``dv`` must be numeric. within : string or list Within-subject factor(s). subject : string Subject identifier. data : dataframe Long-format dataframe. aggregate : string Aggregation method if there are more within-factors in the data than specified in the ``within`` argument. Can be `mean`, `median`, `sum`, `first`, `last`, or any other function accepted by :py:meth:`pandas.DataFrame.groupby`. Returns ------- data : dataframe Dataframe without the missing values. Notes ----- If multiple factors are specified, the missing values are removed on the last factor, so the order of ``within`` is important. In addition, if there are more within-factors in the data than specified in the ``within`` argument, data will be aggregated using the function specified in ``aggregate``. Note that in the default case (aggregation using the mean), all the non-numeric column(s) will be dropped. """

    # Safety checks
    assert isinstance(aggregate, str), 'aggregate must be a str.'
    assert isinstance(within, (str, list)), 'within must be str or list.'
    assert isinstance(subject, str), 'subject must be a string.'
    assert isinstance(data, pd.DataFrame), 'Data must be a DataFrame.'

    idx_cols = _flatten_list([subject, within])
    all_cols = data.columns

    if data[idx_cols].isnull().any().any():
        raise ValueError("NaN are present in the within-factors or in the "
                         "subject column. Please remove them manually.")

    # Check if more within-factors are present and if so, aggregate
    if (data.groupby(idx_cols).count() > 1).any().any():
        # Make sure that we keep the non-numeric columns when aggregating
        # This is disabled by default to avoid any confusion.
        # all_others = all_cols.difference(idx_cols)
        # all_num = data[all_others].select_dtypes(include='number').columns
        # agg = {c: aggregate if c in all_num else 'first' for c in all_others}
        data = data.groupby(idx_cols).agg(aggregate)
    else:
        # Set subject + within factors as index.
        # Sorting is done to avoid performance warning when dropping.
        data = data.set_index(idx_cols).sort_index()

    # Find index with missing values
    if dv is None:
        iloc_nan = data.isnull().values.nonzero()[0]
    else:
        iloc_nan = data[dv].isnull().values.nonzero()[0]

    # Drop the last within level
    idx_nan = data.index[iloc_nan].droplevel(-1)

    # Drop and re-order
    data = data.drop(idx_nan).reset_index(drop=False)
    return data.reindex(columns=all_cols).dropna(how='all', axis=1)