Home > Mobile >  How to select specific element from nested dataframes
How to select specific element from nested dataframes

Time:08-26

I have a list of nested datafrme and I want to exctract the observations of the earliest year, my probleme is: the first year change with the dataframes. the year is either 1992 or 2005.

I want to create a list to stock them, I tried with which,but since there is the same year, observations are repeated, and I want them apart

new_df<- which(df[[i]]==1992 | df[[i]]==2005)

i've tried with ifelse() but I have to do an lm operation after, and it doesn't work. And I can't take only the first rows, because the year are repeated

my code looks like:

df<- list(a<-data.frame(a_1<-(1992:2015),
                      a_2<-sample(1:24)),
        b<-data.frame(b_1<-(1992:2015),
                      b_2<-sample(1:24)),
        c<-data.frame(c_1<-(2005:2015),
                      c_2<-sample(1:11)),
        d<-data.frame(d_1<-(2005:2015),
                      d_2<-sample(1:11)))

CodePudding user response:

You can define a function to get the data on one data.frame and loop on the list to extract values.

Below I use map from the purrr package but you can also use lapply and for loops

Please do not use <- when assigning values in a function call (here data.frame() ) because it will mess colnames. = is used in function calls for arguments variables and it's okay to use it. You can read this ;)

df<- list(a<-data.frame(a_1 = (1992:2015),
                        a_2 = sample(1:24)),
          b<-data.frame(b_1 = (1992:2015),
                        b_2 = sample(1:24)),
          c<-data.frame(c_1 = (2005:2015),
                        c_2 = sample(1:11)),
          d<-data.frame(d_1 = (2005:2015),
                        d_2 = sample(1:11)))

extract_miny <- function(df){
    miny <- min(df[,1])
    res <- df[df[,1] == miny, 2]
    names(res) <- miny
    return(res)
}

map(df, extract_miny)

CodePudding user response:

If the data is sorted as the example, you can slice() the first row for the information

library(tidyverse)

df<- list(a<-data.frame(a_1 = (1992:2015),
                        a_2 = sample(1:24)),
          b<-data.frame(b_1 = (1992:2015),
                        b_2 = sample(1:24)),
          c<-data.frame(c_1 = (2005:2015),
                        c_2 = sample(1:11)),
          d<-data.frame(d_1 = (2005:2015),
                        d_2 = sample(1:11)))

df %>%  
  map(~ slice(.x, 1))

[[1]]
   a_1 a_2
1 1992   7

[[2]]
   b_1 b_2
1 1992  19

[[3]]
   c_1 c_2
1 2005   4

[[4]]
   d_1 d_2
1 2005   5
  • Related