Home > Back-end >  Extract values from multiple data frames at the same time
Extract values from multiple data frames at the same time

Time:08-09

I am trying to extract specific data points from multiple data frames with the same formatting. The data is messy with unwanted values spread throughout. This is an simplified version

    df1     
    a   b   c
1   NA  6   NA
2   3   7   NA
3   NA  NA  4
            
            
    df2     
    a   b   c
1   NA  4   NA
2   6   1   NA
3   NA  NA  7
            
            
    df3     
    a   b   c
1   NA  9   NA
2   8   2   NA
3   NA  NA  4

This code works to extract the desired data out of df1 and combines them into a new data frame with the unwanted data cut out.

v1 <- df1[1,2]
v2 <- df1[2,2]
v3 <- df1[3,3]

df1 <- data.frame(v1, v2, v3)

So now df1 looks like this:

    v1  v2  v3
1   6   7   4

How can I run this same code across all three dataframes in one go? The data points that I want from each data frame are in the same positions for all of them.

CodePudding user response:

Here's a tidyverse solution that selects the same elements of each of the three data frames. I assume that is what is required. It utilises lapply and the fact that dplyr::bind_rows(), when given a list, will bind the elements of the list into a single data frame.

library(tidyverse)

dfList <- list(df1, df2, df3)

lapply(
  dfList,
  function(x) {
    tibble(v1=x$b[1], v2=x$b[2], v3=x$c[3])
  }
) %>% 
bind_rows()
# A tibble: 3 × 3
     v1    v2    v3
  <int> <int> <int>
1     6     7     4
2     4     1     7
3     9     2     4

Next time, please provide your test data in a more conveniently accessible form, perhaps by posting the output from dput(). You will help yourself by helping others to help you.

  • Related