How to Make a List of All Dataframes That Are in My Global Environment

How can I make a list of all dataframes that are in my global environment?

From your posted code, I would recommend you start a new R session, and read the files in again with the following code

do.call(rbind, lapply(list.files(pattern = ".csv"), read.csv))

Looping through data frames in global environment and passing a function to all to create new list

I've figured out the issue was with my function f1 and mget(ls()) in lapply. Here's what worked to solve the problem:

A

dfs_list <- Filter(function(x) is(x, "data.frame"), mget(ls()))

B

f1 <- function(df) { 
df %>%
dplyr::mutate(dplyr::mutate(row = row_number()) %>%
tidyr::pivot_wider(names_from = row, values_from = -row)
}

C

lst1 <- lapply(dfs_list, f1)

Make list of objects in global environment matching certain string pattern

I have used the following, obviously this will need to be repeated for each pattern.

Pattern1<-grep("local",names(.GlobalEnv),value=TRUE)
Pattern1_list<-do.call("list",mget(Pattern1))

How to order multiple dataframes in Global Environment R

Andres, See if this helps. I added a pad of '0' for the max number of characters (e.g. 132 = 3 characters wide):

#Main dataframe
time <- 1901:2032
b <- 1:132
data_ <- data.frame(time,b)


#Loop for creating data_i where i goes from 1 to 132
simulations <- 10000
for (i in 1:132) {
assign(paste("data_",str_pad(i,nchar(max(b)),pad="0"), sep = ""), as.data.frame( sapply(data_[i,], function(n) rep(n,simulations)), stringsAsFactors = FALSE ))
}

#Store all dataframes in list (**I THINK THE PROBLEM IS HERE**)
data_names<-str_extract(ls(), '^data_[[:digit:]]{1,3}$')[!is.na(str_extract(ls(), '^data_[[:digit:]]{1,3}$'))]
dataframes<-lapply(data_names, function(x)get(x))

#Create a new column in each dataframe
new_list <- lapply(dataframes, function(x) cbind(x, production = as.numeric(runif(simulations, min = 50, max = 100))))

#Create data_newi in environnment
list2env(setNames(new_list,paste0("data_new", paste(str_pad(seq_along(dataframes),nchar(max(seq_along(dataframes))),pad="0"),sep=""))),
envir = parent.frame())

How do I make a list of data frames?

This isn't related to your question, but you want to use = and not <- within the function call. If you use <-, you'll end up creating variables y1 and y2 in whatever environment you're working in:

d1 <- data.frame(y1 <- c(1, 2, 3), y2 <- c(4, 5, 6))
y1
# [1] 1 2 3
y2
# [1] 4 5 6

This won't have the seemingly desired effect of creating column names in the data frame:

d1
# y1....c.1..2..3. y2....c.4..5..6.
# 1 1 4
# 2 2 5
# 3 3 6

The = operator, on the other hand, will associate your vectors with arguments to data.frame.

As for your question, making a list of data frames is easy:

d1 <- data.frame(y1 = c(1, 2, 3), y2 = c(4, 5, 6))
d2 <- data.frame(y1 = c(3, 2, 1), y2 = c(6, 5, 4))
my.list <- list(d1, d2)

You access the data frames just like you would access any other list element:

my.list[[1]]
# y1 y2
# 1 1 4
# 2 2 5
# 3 3 6

How to make a functional list with the data.frames from the environment in R?

If we have multiple data.frames in the global environment that we want to merge, we can use mget and ls:

file_1 = data.frame(id = c(1,2), a = c(1,2))
file_2 = data.frame(id = c(1,2), b = c(3,4))
file_3 = data.frame(id = c(3,4), a = c(5,6))

Reduce(\(...) merge(..., all = T), mget(ls(pattern = "file")))
id a b
1 1 1 3
2 2 2 4
3 3 5 NA
4 4 6 NA

How to merge all data frames in the global environment respectively?

You have to replace x with your actual id column in the data to eliminate warning/error messages.

You may do either of two

base R

``` r
master1 <- data.frame(x = LETTERS[1:10], y1 = sample(1:100,10))
master2 <- data.frame(x = LETTERS[1:10], y2 = sample(1:100,10))
master10 <- data.frame(x = LETTERS[1:10], y3 = sample(1:100,10))
DF_obj <- lapply(ls(pattern = ".*master"), get)

gendf <- Reduce(function(.x, .y) merge(.x, .y, by = 'x'), x = DF_obj[-1], init = DF_obj[1])

gendf[, order(names(gendf))]
#> x y1 y2 y3
#> 1 A 37 86 61
#> 2 B 3 23 89
#> 3 C 69 46 95
#> 4 D 16 9 54
#> 5 E 62 85 52
#> 6 F 19 5 35
#> 7 G 55 28 90
#> 8 H 40 52 5
#> 9 I 7 48 100
#> 10 J 48 16 9

tidyverse

master1 <- data.frame(x = LETTERS[1:10], y1 = sample(1:100,10))
master2 <- data.frame(x = LETTERS[1:10], y2 = sample(1:100,10))
master10 <- data.frame(x = LETTERS[1:10], y3 = sample(1:100,10))
DF_obj <- lapply(ls(pattern = ".*master"), get)

library(tidyverse)
purrr::reduce(DF_obj[-1], .init = DF_obj[1], ~ .x %>% as.data.frame() %>% left_join(.y, by = 'x'))
#> x y1 y3 y2
#> 1 A 77 87 93
#> 2 B 10 18 74
#> 3 C 2 89 64
#> 4 D 89 98 5
#> 5 E 13 99 21
#> 6 F 74 25 4
#> 7 G 87 4 22
#> 8 H 62 27 17
#> 9 I 14 10 99
#> 10 J 21 100 78

Created on 2021-05-21 by the reprex package (v2.0.0)

Since the random seed has not been fixed, the results are different in two reprexes.

Loop through dataframes in global environment and apply function to them

Keeping your original formatear function

formatear <- function(eq){
eq$Volume <- NULL
eq$Date <- as.Date(eq$Date)

nt <- eq$Adj.Close[1:nrow(eq)-1]
nt1 <- eq$Adj.Close[2:nrow(eq)]
eq$return <- percent(c(NA, nt1/nt-1), accuracy = 0.0001)
return(eq)
}

You can use mget to get list of dataframes and apply the function with lapply.

clean_list_data <- lapply(mget(dfs), formatear)

clean_list_data should be a list of dataframes in the format that you want. You can access individual dataframes with clean_list_data[[1]], clean_list_data[[2]] and so on. It is easier to manage the data if you keep them in a list like this instead of creating multiple dataframes in the global environment.



Related Topics



Leave a reply



Submit