removing selected variables in R environment
This is certainly a simple question but I can't find a solution.
I want to clean my environment by removing some variables I don't need anymore and keep some others.
I unterstand ls() can list them and ls()[[i]] returns the name of the variable, as a string.
So If I want to remove the 10th, let's say it's the variable age , ls()[[10]] will return "age", and I would like to do something like rm(ls()[[10]), but it does not work. I can't figure out to force rm(ls([10])) to be be equivalent to rm(age).
I guess I need to force some evaluation of string "age" to return the variable age but can't find the proper function in R documentation.
Thanks if you can help.
r
add a comment |
This is certainly a simple question but I can't find a solution.
I want to clean my environment by removing some variables I don't need anymore and keep some others.
I unterstand ls() can list them and ls()[[i]] returns the name of the variable, as a string.
So If I want to remove the 10th, let's say it's the variable age , ls()[[10]] will return "age", and I would like to do something like rm(ls()[[10]), but it does not work. I can't figure out to force rm(ls([10])) to be be equivalent to rm(age).
I guess I need to force some evaluation of string "age" to return the variable age but can't find the proper function in R documentation.
Thanks if you can help.
r
Since you are using a character string for the object name, you wantrm(list=ls()[10]). It's explained in the docs forhelp(rm)
– Rich Scriven
Nov 12 at 21:17
Thanks all for the info. That solves it. For the record, my objective was to remove maybe 90% of the variables. So I will go for a title loop based on the rank in ls(). Naming explicitly the variable to remove kind of defeats the purpose, which was to remove the not desired variables quickly, i.e without explicitly writing the names of the variables to remove.
– Hugues
Nov 13 at 12:17
add a comment |
This is certainly a simple question but I can't find a solution.
I want to clean my environment by removing some variables I don't need anymore and keep some others.
I unterstand ls() can list them and ls()[[i]] returns the name of the variable, as a string.
So If I want to remove the 10th, let's say it's the variable age , ls()[[10]] will return "age", and I would like to do something like rm(ls()[[10]), but it does not work. I can't figure out to force rm(ls([10])) to be be equivalent to rm(age).
I guess I need to force some evaluation of string "age" to return the variable age but can't find the proper function in R documentation.
Thanks if you can help.
r
This is certainly a simple question but I can't find a solution.
I want to clean my environment by removing some variables I don't need anymore and keep some others.
I unterstand ls() can list them and ls()[[i]] returns the name of the variable, as a string.
So If I want to remove the 10th, let's say it's the variable age , ls()[[10]] will return "age", and I would like to do something like rm(ls()[[10]), but it does not work. I can't figure out to force rm(ls([10])) to be be equivalent to rm(age).
I guess I need to force some evaluation of string "age" to return the variable age but can't find the proper function in R documentation.
Thanks if you can help.
r
r
asked Nov 12 at 20:27
Hugues
166
166
Since you are using a character string for the object name, you wantrm(list=ls()[10]). It's explained in the docs forhelp(rm)
– Rich Scriven
Nov 12 at 21:17
Thanks all for the info. That solves it. For the record, my objective was to remove maybe 90% of the variables. So I will go for a title loop based on the rank in ls(). Naming explicitly the variable to remove kind of defeats the purpose, which was to remove the not desired variables quickly, i.e without explicitly writing the names of the variables to remove.
– Hugues
Nov 13 at 12:17
add a comment |
Since you are using a character string for the object name, you wantrm(list=ls()[10]). It's explained in the docs forhelp(rm)
– Rich Scriven
Nov 12 at 21:17
Thanks all for the info. That solves it. For the record, my objective was to remove maybe 90% of the variables. So I will go for a title loop based on the rank in ls(). Naming explicitly the variable to remove kind of defeats the purpose, which was to remove the not desired variables quickly, i.e without explicitly writing the names of the variables to remove.
– Hugues
Nov 13 at 12:17
Since you are using a character string for the object name, you want
rm(list=ls()[10]). It's explained in the docs for help(rm)– Rich Scriven
Nov 12 at 21:17
Since you are using a character string for the object name, you want
rm(list=ls()[10]). It's explained in the docs for help(rm)– Rich Scriven
Nov 12 at 21:17
Thanks all for the info. That solves it. For the record, my objective was to remove maybe 90% of the variables. So I will go for a title loop based on the rank in ls(). Naming explicitly the variable to remove kind of defeats the purpose, which was to remove the not desired variables quickly, i.e without explicitly writing the names of the variables to remove.
– Hugues
Nov 13 at 12:17
Thanks all for the info. That solves it. For the record, my objective was to remove maybe 90% of the variables. So I will go for a title loop based on the rank in ls(). Naming explicitly the variable to remove kind of defeats the purpose, which was to remove the not desired variables quickly, i.e without explicitly writing the names of the variables to remove.
– Hugues
Nov 13 at 12:17
add a comment |
2 Answers
2
active
oldest
votes
How about the following:
1: Grab the list in the environment,
2: Define the items you want to remove,
3: Filter the list by the items you want to remove
4: Then remove them
list <- ls()
to_remove <- c("Item1", "Item2")
list_to_remove <- list[ list %in% to_remove]
list_to_remove
rm(list=list_to_remove)
add a comment |
The list argument of rm will help you. It accepts a character vector. Consider:
age <- 1
rm(list = "age") # Same effect as rm(age)
age
#Error: object 'age' not found
So running e.g.
rm(list = ls())
will clear all visible objects in the specified environment.
In your case rm(list = ls()[10]) will do what you want. However, note that ls() always returns a sorted character vector, so the 10th entry can change rather easily. You probably want to do the following
objects_to_remove <- c("age", "another_object") # etc
rm(list = objects_to_remove)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269605%2fremoving-selected-variables-in-r-environment%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
How about the following:
1: Grab the list in the environment,
2: Define the items you want to remove,
3: Filter the list by the items you want to remove
4: Then remove them
list <- ls()
to_remove <- c("Item1", "Item2")
list_to_remove <- list[ list %in% to_remove]
list_to_remove
rm(list=list_to_remove)
add a comment |
How about the following:
1: Grab the list in the environment,
2: Define the items you want to remove,
3: Filter the list by the items you want to remove
4: Then remove them
list <- ls()
to_remove <- c("Item1", "Item2")
list_to_remove <- list[ list %in% to_remove]
list_to_remove
rm(list=list_to_remove)
add a comment |
How about the following:
1: Grab the list in the environment,
2: Define the items you want to remove,
3: Filter the list by the items you want to remove
4: Then remove them
list <- ls()
to_remove <- c("Item1", "Item2")
list_to_remove <- list[ list %in% to_remove]
list_to_remove
rm(list=list_to_remove)
How about the following:
1: Grab the list in the environment,
2: Define the items you want to remove,
3: Filter the list by the items you want to remove
4: Then remove them
list <- ls()
to_remove <- c("Item1", "Item2")
list_to_remove <- list[ list %in% to_remove]
list_to_remove
rm(list=list_to_remove)
answered Nov 12 at 20:33
user113156
7911417
7911417
add a comment |
add a comment |
The list argument of rm will help you. It accepts a character vector. Consider:
age <- 1
rm(list = "age") # Same effect as rm(age)
age
#Error: object 'age' not found
So running e.g.
rm(list = ls())
will clear all visible objects in the specified environment.
In your case rm(list = ls()[10]) will do what you want. However, note that ls() always returns a sorted character vector, so the 10th entry can change rather easily. You probably want to do the following
objects_to_remove <- c("age", "another_object") # etc
rm(list = objects_to_remove)
add a comment |
The list argument of rm will help you. It accepts a character vector. Consider:
age <- 1
rm(list = "age") # Same effect as rm(age)
age
#Error: object 'age' not found
So running e.g.
rm(list = ls())
will clear all visible objects in the specified environment.
In your case rm(list = ls()[10]) will do what you want. However, note that ls() always returns a sorted character vector, so the 10th entry can change rather easily. You probably want to do the following
objects_to_remove <- c("age", "another_object") # etc
rm(list = objects_to_remove)
add a comment |
The list argument of rm will help you. It accepts a character vector. Consider:
age <- 1
rm(list = "age") # Same effect as rm(age)
age
#Error: object 'age' not found
So running e.g.
rm(list = ls())
will clear all visible objects in the specified environment.
In your case rm(list = ls()[10]) will do what you want. However, note that ls() always returns a sorted character vector, so the 10th entry can change rather easily. You probably want to do the following
objects_to_remove <- c("age", "another_object") # etc
rm(list = objects_to_remove)
The list argument of rm will help you. It accepts a character vector. Consider:
age <- 1
rm(list = "age") # Same effect as rm(age)
age
#Error: object 'age' not found
So running e.g.
rm(list = ls())
will clear all visible objects in the specified environment.
In your case rm(list = ls()[10]) will do what you want. However, note that ls() always returns a sorted character vector, so the 10th entry can change rather easily. You probably want to do the following
objects_to_remove <- c("age", "another_object") # etc
rm(list = objects_to_remove)
edited Nov 12 at 20:40
answered Nov 12 at 20:29
Anders Ellern Bilgrau
6,2431729
6,2431729
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269605%2fremoving-selected-variables-in-r-environment%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Since you are using a character string for the object name, you want
rm(list=ls()[10]). It's explained in the docs forhelp(rm)– Rich Scriven
Nov 12 at 21:17
Thanks all for the info. That solves it. For the record, my objective was to remove maybe 90% of the variables. So I will go for a title loop based on the rank in ls(). Naming explicitly the variable to remove kind of defeats the purpose, which was to remove the not desired variables quickly, i.e without explicitly writing the names of the variables to remove.
– Hugues
Nov 13 at 12:17