0

I have 50 data frames with temperature and humidity data and I want to create a new data frame, which includes a specific row from each data frame. Firstly I've created a list (onomata_list) in which I imported all 50 data frames, secondly I've created a dummy data frame (All_stations_30_6_17) to "send" the rows from each data frame and finally I wrote the following for loop in order to achieve the abovementioned:

 for(i in 1:length(onomata_list)){
  new_df <- onomata_list[[i]] %>% filter(date == "2017-06-30" & time == "17:00")
  All_Stations_30_6_17 <- rbind(All_stations_30_6_17,new_df)
}

Unfortunately it doesn't work, and I receive the following message:

Error in rbind(All_stations_30_6_17, new_df) : object 'All_stations_30_6_17' not found

It tried to solve it for the last couple of hours, but I had no luck. Any suggestion ?

1
  • In the code you have posted, you have not created the variable All_stations_30_6_17 before you use it. It would be helpful if you could post a small reproducible example. Commented May 20, 2021 at 21:02

1 Answer 1

1

This should work if you have a list of dataframes:

library(purrr)
library(dplyr)

All_Stations_30_6_17 <- map_dfr(onomata_list, ~ filter(.x, date == "2017-06-30" & time == "17:00"))

Map functions generally iterate through list elements. Here map_drc will map the purrr-style function (denoted with the ~) over the list elements of onomata_list and then row bind (hence the _dfr) into a dataframe output.

Sign up to request clarification or add additional context in comments.

5 Comments

It works perfectly. Thank you very much not only for the solution, but for introducing me this new function!!
@H.Johnson The tidyverse is a collection of several very useful packages for data manipulation. purrr and dplyr are two of those packages, but I think you would find it helpful to learn these packages.
I will definitely do so LMc! Thanks again!!
hi again! I would like a little clarification regarding your code. It works perfectly if I want to create 1 dataframe from the contents of a list, but If I want to convert each list's element into a new dataframe, but keeping the filter for each and every new dataframe, can I do it with an alteration of the abovementioned script ? I've been trying it for about 2 hours but I haven't manage to achieve it..
@H.Johnson then I would just use map instead of map_dfr, which is what converts the list into a single, stacked dataframe.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.