Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create list of dataframes matching a pattern

This is a very simple question, however I can't seem to come up w/ an answer. I would like to create a list of data frames matching a pattern, then rm these from the global environment.

The pattern to match is 'water_land_by_owntype_*'

This is what I have tried, but it doesn't work...I think b/c it doesn't know where to search for the string.

rm (matches <- list(
    grep('water_land_by_owntype_*')))

-al

like image 881
cherrytree Avatar asked Mar 19 '23 19:03

cherrytree


1 Answers

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_.*"))
like image 50
Victorp Avatar answered Apr 30 '23 18:04

Victorp