Search code examples
rlistfiltermultiplicationdivide

How to multiply and divide all values in a vector using a list in R and how to filter a list


I have a list (of 83 data frames) and would like to multiply one of the variables (i.e. Mn, see 'practice' df below) by 10000, across the entire list. I would then like to find the maximum value of Mn (within each data frame) and then retain this value (the maximum value) and all values below it (within each data frame). Along with these Mn values, I would like to retain the corresponding values from all other variables within the data frames (i.e. Ba, Sr). I want to exclude all values above this point from each data frame, for all variables. Once I have done this reduction I would then like to return the Mn to it's initial values (i.e. divide Mn by 10000).

Below is the practice script and what I would like to achieve (with Mn being multiplied by 10000 beforehand and then divided by 10000 afterwards). Any assistance would be greatly appreciated.

Ba <- c(1,1,1,2,2)
Sr <- c(1,1,1,2,2)
Mn <- c(1,1,2,1,1)
df <- data.frame(Ba, Sr, Mn)

df

#   Ba Sr Mn
# 1  1  1  1
# 2  1  1  1
# 3  1  1  2
# 4  2  2  1
# 5  2  2  1

This is what I want to achieve in R:

Ba <- c(1,2,2)
Sr <- c(1,2,2)
Mn <- c(2,1,1)
df <- data.frame(Ba, Sr, Mn)

df

#     Ba  Sr  Mn
# 1   1   1   2
# 2   2   2   1
# 3   2   2   1

I think the script for the filter part of my question is something like this:

MaxMn <- lapply(df, function(df){
  df %>% 
   filter(cummax(invoke(pmax, cur_data())) == max(cur_data()))
})

Solution

  • To select all the rows from the max value in Mn column you can use filter as -

    library(dplyr)
    
    df %>% filter(row_number() >= which.max(Mn))
    
    #  Ba Sr Mn
    #1  1  1  2
    #2  2  2  1
    #3  2  2  1
    

    with slice -

    df %>% slice(which.max(Mn):n())
    

    and in base R -

    df[which.max(Mn):nrow(df), ]
    

    You can apply this to list of dataframes using lapply/map -

    lapply(list_df, function(df) df %>% filter(row_number() >= which.max(Mn)))