Home > other >  Combining a list of data frames into a new data frame in R
Combining a list of data frames into a new data frame in R

Time:01-17

This is a 3rd edit to the question (leaving below thread just in case):

The following code makes some sample data frames, selects those with "_areaX" in the title and makes a list of them. The goal is then to combine the data frames in the list into 1 data frame. It almost works...

Area1 <- 100
Area2 <- 200
Area3 <- 300
Zone <- 3

a1_areaX <- data.frame(Area1)
a2_areaX <- data.frame(Area2)
a3_areaX <- data.frame(Area3)
a_zoneX <- data.frame(Zone)

library(dplyr)   
pattern = "_areaX"
df_list <- mget(ls(envir = globalenv(), pattern = pattern))
big_data = bind_rows(df_list, .id = "FileName")

The problem is the newly created data frame looks like this:

newly created data frame

And I need it to look like this:

File Name Area measurement
a1_areaX 100
a2_areaX 200
a3_areaX 300

Below are my earlier attempts at asking this question. Edited from first version:

I have csv files imported into R Global Env that look like this (I'd share the actual file(s) but there doesn't seem to be a way to do this here):

sample data frame

They all have a name, the above one is called "s6_section_area". There are many of them (with different names) and I've put them all together into a list using this code:

pattern = "section_area"
section_area_list <- list(mget(grep(pattern,ls(globalenv()), value = TRUE), globalenv()))

Now I want a new data frame that looks like this, put together from the data frames in the above made list.

File Name Area measurement
a1_section_area a number
a2_section_area another number
many more more numbers

So, the first column should list the name of the original file and the second column the measurement provided in that file.

Hope this is clearer - Not sure how else to provide reproducible example without sharing the actual files (which doesn't seem to be an option).

addition to edit: Using this code

section_area_data <- bind_rows(section_area_list, .id = "FileName")

I get (it goes on and on to the right)

new data frame

I'm after a table that looks like the sample above, left column is file name with a list of file names going down. Right column is the measurement for that file name (taken from original file).

CodePudding user response:

Note that in your list of dataframes (df_list) all the columns have different names (Area1, Area2, Area3) whereas in your output dataframe they all have been combined into one single column. So for that you need to change the different column names to the same one and bind the dataframes together.

library(dplyr)
library(purrr)

result <- map_df(df_list, ~.x %>% 
                  rename_with(~"Area", contains('Area')), .id = 'FileName')
result

#  FileName Area
#1 a1_areaX  100
#2 a2_areaX  200
#3 a3_areaX  300

CodePudding user response:

Thanks everyone for your suggestions. In the end, I was able to combine the suggestions and some more thinking and came up with this, which works perfectly.

library("dplyr")                   
                  
pattern = "section_area"
section_area_list <- mget(ls(envir = globalenv(), pattern = pattern))  
section_area_data <- bind_rows(section_area_list, .id = "FileName") %>%
    select(-V1) 

So, a bunch of csv files were imported into R Global Env. A list of all files with a name ending in "section_area" was made. Those files were than bound into one big data frame, with the file names as one column and the value (area measurement in this case) in the other column (there was a pointless column in the original csv files called "V1" which I deleted).

This is what one of the many csv files looks like sample csv file

And this is the layout of the final data frame (it goes on for about 150 rows) final data frame

  •  Tags:  
  • Related