Make List of Objects in Global Environment Matching Certain String Pattern

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 find objects in the global environment that match a pattern of two underscores in the object name

Sample environment:

vec <- c("mom_big_p1", "mom_big_p2", "mom_big_p3", "mom_small_p1", "mom_small_p2", "mom_small_p3", "mom_big_rank")
sapply(vec, assign, 1, env=environment())

ls()
# [1] "mom_big_p1" "mom_big_p2" "mom_big_p3" "mom_big_rank" "mom_small_p1" "mom_small_p2"
# [7] "mom_small_p3" "vec"

List objects that match your pattern:

ls(pattern = "_.+_p\\d+$")
# [1] "mom_big_p1" "mom_big_p2" "mom_big_p3" "mom_small_p1" "mom_small_p2" "mom_small_p3"

If you need something before the first underscore, try a pattern of ".+_.+_p\\d+$".

Combine R objects in global environment into a list

With the output, we can use mget to return a list of the values from the string object/objects

lst1 <- mget(ls(pattern = '^data_'))

Put all dataframes with names containing a particular string into a single list

That is because the function creates another "function environment", and since you call ls() from within this function, you will have the list of objects existing within this function. Please consider the following code which sould be clear.

print(ls()) # Globalenv objects
test <- function() { #different envir inside a function
a <- "test"
print(ls())
}
test() # only prints "a"
test <- function() {
print(ls(.GlobalEnv))
}
test() # prints all the globalenv objects

Actually, functions use up to as many as four types of environments: enclosing, binding, execution and calling. You can learn more in this in-depth Chapter of Avanced R by Hadley.

create list of dataframes matching a pattern

Hi you can do like this :

# Create some data.frame
water_land_by_owntype_1 <- mtcars
water_land_by_owntype_2 <- mtcars
water_land_by_owntype_3 <- mtcars
water_land_by_owntype_4 <- mtcars
water_land_by_owntype_5 <- mtcars

# Put them in a list
water_land_by_owntype <- lapply(ls(pattern = "water_land_by_owntype_.*"), get)

# or more directly
water_land_by_owntype <- mget(ls(pattern = "water_land_by_owntype_.*"))

# Delete them
rm(list = ls(pattern = "water_land_by_owntype_.*"))

Return elements of list as independent objects in global environment

There is special function for mapping list to environment:

> obj <- list(a=1:5, b=2:10, c=-5:5)
> ls()
[1] "obj"
> list2env(obj,globalenv())
<environment: R_GlobalEnv>
> ls()
[1] "a" "b" "c" "obj"

P. S. It is my comment provided as an answer

How to get a list data that are in global environment into a list

We can use mget with ls and specify the pattern with "_101" as the end ($) of the object name. It would get the values of all those objects into a list

lst1 <- mget(ls(pattern = "_101$"))

Get colnames from objects in global environment (With specific pattern), then just return what's new

We can use map2 to do the setdiff here to return a list column by comparing the list column with the lag of the list column

library(dplyr)
library(purrr)
Obj_Size %>%
mutate(new_col = map2(colnames, lag(colnames), setdiff), colnames = NULL)

-output

#          Obj_name nrow ncol                                                       new_col
#1 IRIS1_St 150 5 Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, Species
#2 IRIS2_Db 150 6 Petal.Length2
#3 IRIS3_Sum 150 7 Sepal.sum
#4 IRIS4_Change 89 8 SL.Change
#5 IRIS10_bananas 89 9 bananas

If it needs to be a character column, use

library(stringr)
Obj_Size %>%
mutate(new_col = map2_chr(colnames, lag(colnames),
~ str_c(setdiff(.x, .y), collapse=", ")), colnames = NULL)

-output

#      Obj_name nrow ncol                                                       new_col
#1 IRIS1_St 150 5 Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, Species
#2 IRIS2_Db 150 6 Petal.Length2
#3 IRIS3_Sum 150 7 Sepal.sum
#4 IRIS4_Change 89 8 SL.Change
#5 IRIS10_bananas 89 9 bananas

Or using base R with Map

Obj_Size$new_col <- Map(setdiff, Obj_Size$colnames, c(NA, head(Obj_Size$colnames,-1)))

Removing Files from Global Environment with a Certain Pattern

You can make use of pattern inside of the remove function:

rm(list = ls(pattern = "^samp"))

Or using grep:

rm(list = grep("^samp", ls(), value = TRUE))


Related Topics



Leave a reply



Submit