def pca_fit(X, R, n_components=None):
    """Perform PCA on X with rotation matrix R.

    # Parameters
    X : np.ndarray
        an array of data with (N x num of features)
    R : np.ndarray
        an rotation matrix
    n_components : int
        number of components retained

    # Returns
    X_reduce : np.ndarray
        the array with reduced dimension.
    """
    n_dim = X.shape[1]
    if n_components is not None:
        assert n_components <= n_dim

    # transpose
    X_trans = X.dot(R)

    # select
    X_reduce = X_trans[:, :n_components]

    return X_reduce