Remove all Environment elements containing numbers and uppercase letters

Asked

Viewed 164 times

8

Consider the objects:

vectorA<-1:10
vectorU<-1:10
vector<-1:10
vector1<-1:10
vector86<-1:10
  • How to remove objects containing numbers?

  • How to remove objects containing uppercase letters?

My goal is to do this at once, in such a way that there is onlyvector. I don’t want to have to type, for example:

rm(vector1,vector86...)

I want to wear something like: [0-9] and [A-Z] within the function rm.

In addition, how would the function for a list? Follows dputof the same to assist the answer:

mylist=structure(list(dataset1 = structure(list(var1 = 
c(52.8239523619413, 
67.412983905524, 85.6350479647517, 69.0797958150506, 23.8283388130367, 
95.553572345525, 29.0164476074278, 71.0307511687279, 40.3978266194463
), var2 = c(29.2814019694924, 61.8766243383288, 27.239320538938, 
27.597702536732, 61.7392177134752, 79.012787565589, 57.9802662879229, 
80.5304721556604, 64.3874954432249), var3 = c(28.0727723427117, 
60.635687764734, 28.839645255357, 21.5680585056543, 41.4972599223256, 
78.5253136977553, 71.1393622308969, 43.5780669748783, 50.4048792645335
), var4 = c(63.3389138430357, 39.6925644949079, 64.7707579657435, 
49.8857426457107, 24.3227748572826, 20.8205243013799, 
34.4325823150575, 
24.2039945721626, 28.1429144367576), var5 = c(68.8740702904761, 
55.5163130350411, 24.4956089183688, 35.9698024578393, 41.842991374433, 
93.4637849777937, 78.2090006582439, 63.6315332911909, 35.8201904967427
), var6 = c(49.4833218678832, 79.0594268217683, 84.8702448047698, 
89.154454190284, 45.2969283424318, 95.3247463144362, 58.4594663605094, 
97.3974690772593, 81.7770860157907), kmeans = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", "2", "3"), class = 
"factor")), .Names = c("var1", 
"var2", "var3", "var4", "var5", "var6", "kmeans"), row.names = c(1L, 
3L, 8L, 10L, 15L, 16L, 17L, 24L, 25L), class = "data.frame"), 
mydata = structure(list(var1 = c(68.8994040898979, 86.4890068769455, 
41.4491515234113, 48.7125792913139, 67.0896042510867, 
89.0794235467911, 
41.5327339619398, 78.3874392323196, 47.4415177851915, 83.976521063596, 
23.752948474139, 20.1965479925275, 65.0721608288586, 88.8870289362967, 
74.9182807281613, 84.6756487898529, 99.3809685111046, 
80.1436454802752, 
97.8404737263918), var2 = c(32.7941456995904, 64.6668173931539, 
34.7624584287405, 77.6456462778151, 73.1857040151954, 
28.4563537873328, 
86.6737443953753, 92.1690583974123, 96.1259284429252, 
95.3986377082765, 
54.7287909500301, 61.9130505435169, 92.1239553764462, 
97.6163926720619, 
91.1119207553566, 59.7223405726254, 56.271854378283, 63.5032017342746, 
97.2438823990524), var3 = c(96.9139205291867, 32.3409661464393, 
94.5406094565988, 52.3079419881105, 92.548458352685, 82.7145664207637, 
83.1966380216181, 53.0209177173674, 56.4625365659595, 
83.9760917425156, 
92.0599205233157, 55.0897601060569, 40.1735444366932, 
29.7791076265275, 
88.4056675434113, 71.2447142973542, 46.5862010978162, 
44.6803763508797, 
81.6879714652896), var4 = c(70.233196914196, 50.9632669016719, 
68.2954710535705, 77.7710892260075, 98.3270382508636, 59.908417519182, 
92.4725500866771, 31.0966997593641, 78.1473434716463, 
68.3402911759913, 
62.4164910800755, 83.3351655490696, 59.4744057022035, 
75.3354255855083, 
82.4973624758422, 69.706082995981, 86.8536845035851, 78.9299669303, 
49.516792036593), var5 = c(68.4397785365582, 86.5204527787864, 
24.5654080621898, 89.3807902187109, 64.756205175072, 85.9483086690307, 
25.5898364260793, 78.8896332494915, 87.7399998344481, 
22.7459753118455, 
42.6951446570456, 49.3495168350637, 41.639910582453, 98.5095219127834, 
43.6992948688567, 82.0324843563139, 97.1282077953219, 
67.0289828069508, 
83.0017600394785), var6 = c(35.3501682728529, 31.1327565461397, 
21.9722577184439, 44.7750159911811, 41.9370178319514, 
53.7735476344824, 
35.3551966138184, 38.252913877368, 34.6147643961012, 32.9839759506285, 
28.1436816416681, 24.9134236015379, 26.697716023773, 31.1821212992072, 
29.7940192557871, 68.7213623709977, 72.2950306721032, 
45.6320049799979, 
38.598245754838), kmeans = structure(c(2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = 
c("1", 
"2", "3"), class = "factor")), .Names = c("var1", "var2", 
"var3", "var4", "var5", "var6", "kmeans"), row.names = c(2L, 
4L, 5L, 6L, 7L, 9L, 12L, 13L, 14L, 18L, 19L, 20L, 21L, 22L, 
23L, 26L, 27L, 29L, 30L), class = "data.frame"), dataset2 = 
structure(list(
    var1 = c(24.4858060218394, 38.8159691542387), var2 = 
c(92.535456828773, 
    92.1455484442413), var3 = c(23.7727256864309, 39.6465750969946
    ), var4 = c(81.2065978161991, 62.8835928440094), var5 = 
c(47.5475750491023, 
    45.1348656974733), var6 = c(90.111320503056, 79.97005013749
    ), kmeans = structure(c(3L, 3L), .Label = c("1", "2", 
    "3"), class = "factor")), .Names = c("var1", "var2", 
"var3", "var4", "var5", "var6", "kmeans"), row.names = c(11L, 
28L), class = "data.frame")), .Names = c("dataset1", "mydata", 
"dataset2"))

2 answers

8


You can do it this way:

library(purrr)
library(stringr)
remover <- ls() %>% keep(~str_detect(.x, "[:digit:]|[:upper:]"))
rm(list = remover)

The function ls() lists all the variables of the environment. Then we only make a filter to keep in the list those that have some digit or uppercase letter.

We use the function rm with the argument list to be able to pass strings with the name of the variables.

Remember that modifying the global environment in this way is not a good practice. Your code would probably look better if you used a named list.

In the case of the list

With the list I would do something like this:

discard(mylist, str_detect(names(mylist), "[:digit:]"))

Or even more succinctly:

mylist %>% discard(str_detect(names(.), "[:digit:]"))
  • Daniel, what would be the solution with purrr::keep to a list? Try to run here only to delete objects containing numbers, but I could not. See: remove<-mylist%>%keep(~str_detect(.x,'[[:digit:]]')). This generates an error message.

  • Error: Result 1 is not a length 1 atomic vector&#xA;In addition: Warning message:&#xA;In stri_detect_regex(string, pattern, opts_regex = opts(pattern)) :&#xA; argument is not an atomic vector; coercing

  • The list is named and contains three elements.

  • I edited the question.

  • 1

    updated the response

  • Thank you, Daniel. It works perfectly. I will study these notations better on purrr.

Show 1 more comment

7

You can do that with the R also, by means of the following code:

ls()
[1] "vector"   "vector1"  "vector86" "vectorA"  "vectorU"

The function ls() lists all environment variables and their result is a vector. Therefore, it has indexes that can be accessed. We want a way to get to indexes 2, 3, 4 and 5, which are those that have at least one digit or at least one capital letter. For this, we will use the regular expression [[:digit:]]|[[:upper:]], which precisely means finding

  • one digit :digit:

  • or |

  • a capital letter :upper:

Combining this regular expression with the function grep, we can find the indexes of the vector ls() who satisfy our condition:

grep(pattern="[[:digit:]]|[[:upper:]]", ls())
[1] 2 3 4 5

We can search these indexes in the vector ls() original:

ls()[grep(pattern="[[:digit:]]|[[:upper:]]", ls())]
[1] "vector1"  "vector86" "vectorA"  "vectorU"

Finally, just take the above output and use it in the function rm to remove objects we no longer want in Workspace:

rm(list=ls()[grep(pattern="[[:digit:]]|[[:upper:]]", ls())])
ls()
[1] "vector"

An easier way to solve the problem is by using the suggestion placed in the comments below:

rm(grep(pattern="[[:digit:][:upper:]]", ls(), value = TRUE))
  • 1

    Simpler? grep(pattern="[[:digit:][:upper:]]", ls(), value = TRUE). Instead of the disjunction |, have the alternative patterns within the [] gives the same result. And instead of two calls to the function ls(), grep(..., value = TRUE).

  • Good. I edited my reply with this suggestion.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.