创建与模式匹配的数据框列表

时间:2014-08-07 14:59:27

标签: r pattern-matching rm

这是一个非常简单的问题,但我似乎无法回答。我想创建一个匹配模式的data frames列表,然后从全局环境创建rm个列表。

要匹配的模式是“water_land_by_owntype_*

这是我尝试过的,但它不起作用......我想b / c它不知道在哪里搜索字符串。

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

-al

2 个答案:

答案 0 :(得分:5)

你可以这样做:

# 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_.*"))

答案 1 :(得分:0)

这可能是最简单的方法。 1.用ls()提取变量 2.检测(返回布尔)模式
3.找到和子集
4.删除

library(stringr)
a = ls()
index = which(str_detect(ls, "water_land_by_owntype_"))
b = a[index]
rm(b)

希望这有帮助,

相关问题