R: Collect the i-th or Last Rows of Each Data Frame in a List of Data Frames

    Date:

    This post demonstrates selecting either the last row or the i-th row from each data frame in a list of data frames in R.

    the i-th or last rows of data frames within a list

    In the following R code, the function collect_ith_or_last_rows() provides a convenient method for selecting either the i-th or last rows of data frames within a list.

    # list of data.frames or matrices
    list_df_coef <- list(
        L = matrix(c(
            -0.857, -0.806, -0.731, -0.645,
            -0.260, -0.265, -0.262, -0.253,
            -0.407, -0.413, -0.408, -0.395), 
            ncol = 4, byrow = TRUE),
        S = matrix(c(
            0.001, 0.002, 0.003, 0.004,
            0.771, 0.772, 0.773, 0.774,
            -0.015, -0.025, -0.035, -0.045), 
            ncol = 4, byrow = TRUE),
        C = matrix(c(
            -0.181, -0.178, -0.175, -0.172,
            0.045, 0.045, 0.045, 0.045,
            0.000, 0.001, 0.001, 0.001), 
            ncol = 4, byrow = TRUE)
    )
     
    list_df_coef
     
    #---------------------------------------------------------------
    # When ith_row = i, it selects the ith rows.
    # When ith_row is NULL, it defaults to selecting the last rows.
    #---------------------------------------------------------------
    collect_ith_or_last_rows <- function(df_list, ith_row = NULL) {
        # Function to get ith or last row of each data frame
        if (is.null(ith_row)) {
            get_selected_rows <- function(df) {tail(df, n = 1)}
        } else {
            get_selected_rows <- function(df) {df[ith_row,,drop=FALSE]}
        }
        # Apply the function to each data frame in the list
        selected_rows <- lapply(df_list, get_selected_rows)
        # Combine the selected rows into one data frame
        combined_df <- do.call(rbind, selected_rows)
        # Set row names based on the names of list ingredients
        rownames(combined_df) <- names(df_list)
        return(combined_df)
    }
     
    collect_ith_or_last_rows(list_df_coef, 1)
    collect_ith_or_last_rows(list_df_coef)

    The selected results are as follows:

    > 
    > list_df_coef
    $L
           [,1]   [,2]   [,3]   [,4]
    [1,] -0.857 -0.806 -0.731 -0.645
    [2,] -0.260 -0.265 -0.262 -0.253
    [3,] -0.407 -0.413 -0.408 -0.395
     
    $S
           [,1]   [,2]   [,3]   [,4]
    [1,]  0.001  0.002  0.003  0.004
    [2,]  0.771  0.772  0.773  0.774
    [3,] -0.015 -0.025 -0.035 -0.045
     
    $C
           [,1]   [,2]   [,3]   [,4]
    [1,] -0.181 -0.178 -0.175 -0.172
    [2,]  0.045  0.045  0.045  0.045
    [3,]  0.000  0.001  0.001  0.001
     
    > collect_ith_or_last_rows(list_df_coef, 1)
        [,1]   [,2]   [,3]   [,4]
    L -0.857 -0.806 -0.731 -0.645
    S  0.001  0.002  0.003  0.004
    C -0.181 -0.178 -0.175 -0.172
     
    > collect_ith_or_last_rows(list_df_coef)
        [,1]   [,2]   [,3]   [,4]
    L -0.407 -0.413 -0.408 -0.395
    S -0.015 -0.025 -0.035 -0.045
    C  0.000  0.001  0.001  0.001
    > 

    Originally posted on the SHLee AI Financial Model blog.

    Disclosure: Interactive Brokers

    Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

    This material is from SHLee AI Financial Model and is being posted with its permission. The views expressed in this material are solely those of the author and/or SHLee AI Financial Model and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

    Go Source

    Chart

    Sign up for Breaking Alerts

    Share post:

    Popular

    More like this
    Related

    Highlights from the IBKR Quant Blog – May 2024

    Your Privacy When you visit any website it may use...

    Not With a Bang But a Whimper

    Your Privacy When you visit any website it may use...

    Take Notes: Orbisa on GME

    Your Privacy When you visit any website it may use...

    Global Equity Funds Rally As Fed Rate Cuts Loom

    Your Privacy When you visit any website it may use...