Understanding Element Wise Clearing of R's Workspace

Understanding element wise clearing of R's workspace

From ?rm, "Details" section:

Earlier versions of R incorrectly claimed that supplying a character vector in ... removed the objects named in the character vector, but it removed the character vector. Use the list argument to specify objects via a character vector.

Your attempt should have been:

rm(list = WS)

HOWEVER, this will still leave you with an object (a character vector) named "WS" in your workspace since that was created after you called WS <- c(ls()). To actually get rid of the "WS" object, you would have had to use rm(WS, list = WS). :-)


How does it work? If you look at the code for rm, the first few lines of the function captures any individual objects that have been specified, whether quoted or unquoted. Towards the end of the function, you will find the line list <- .Primitive("c")(list, names) which basically creates a character vector of all of the objects individually named and any objects in the character vector supplied to the "list" argument.


Update

Based on your comment, it sounds like you're trying to write a function like:

.clc <- function() {
rm(list = ls(.GlobalEnv), envir = .GlobalEnv)
}

I think it's a little bit of a dangerous function, but let's test it out:

ls()
# character(0)
for (i in 1:5) assign(letters[i], i)
ls()
# [1] "a" "b" "c" "d" "e" "i"
.clc()

ls()
# character(0)

Note: FYI, I've named the function .clc (with a dot) so that it doesn't get removed when the function is run. If you wanted to write a version of the function without the ., you would probably do better to put the function in a package and load that at startup to have the function available.

Clearing all user-defined objects in R workspace

it is a bit dangerous but:

rm(list=ls())

really, don't do this.

rm(list=ls()) doesn't completely clear the workspace

attach() does not make copies of x and y in your global environment, it attaches a dataframe to the search path.

From ?attach:

The database is not actually attached.  Rather, a new environment
is created on the search path and the elements of a list
(including columns of a data frame) or objects in a save file or
an environment are _copied_ into the new environment. If you use
‘<<-’ or ‘assign’ to assign to an attached database, you only
alter the attached copy, not the original object. (Normal
assignment will place a modified version in the user's workspace:
see the examples.) For this reason ‘attach’ can lead to
confusion.

For example:

> search()
[1] ".GlobalEnv" "package:stats" "package:graphics"
[4] "package:grDevices" "package:utils" "package:datasets"
[7] "package:methods" "Autoloads" "package:base"
> a <- data.frame(stuff=rnorm(100))
> search()
[1] ".GlobalEnv" "package:stats" "package:graphics"
[4] "package:grDevices" "package:utils" "package:datasets"
[7] "package:methods" "Autoloads" "package:base"
> attach(a)
> search()
[1] ".GlobalEnv" "a" "package:stats"
[4] "package:graphics" "package:grDevices" "package:utils"
[7] "package:datasets" "package:methods" "Autoloads"
[10] "package:base"
> rm(list=ls())
> search()
[1] ".GlobalEnv" "a" "package:stats"
[4] "package:graphics" "package:grDevices" "package:utils"
[7] "package:datasets" "package:methods" "Autoloads"
[10] "package:base"
> stuff
[1] -0.91436377 0.67397624 0.62891651 -0.99669584 2.07692590 -0.62702302
[...]
> detach(a)
> search()
[1] ".GlobalEnv" "package:stats" "package:graphics"
[4] "package:grDevices" "package:utils" "package:datasets"
[7] "package:methods" "Autoloads" "package:base"

How to perform element-wise statistics over numerous (distance) matrices

Try

lst2 <- lapply(lst1, as.matrix)
dim1 <- sapply(lst2, dim)[,1]
l <- length(lst1)
ar1 <- array(unlist(lst2), dim=c(dim1, l))

as.dist(apply(ar1, 1:2, sum))
as.dist(apply(ar1, 1:2, mean))
as.dist(apply(ar1, 1:2, sd))

data

set.seed(24)
lst1 <- lapply(1:4, function(i) dist(sample(1:10,4, replace=TRUE)))

rm(list = ls()) doesn't work inside a function. Why?

rm is actually working, however since you're using it inside a function, it only removes all objects pertaining to the environment of that function.

Add envir = .GlobalEnv parameter to both calls:

rm(list = ls(envir = .GlobalEnv), envir = .GlobalEnv)

should do it.

I also recommend you take a look at this other question about gc() as i believe it's not a good practice to call it explicitly unless you really need it.

Performing element-wise standard deviation in R with two matrices

Or you can do base R:

matrix(mapply(function(x,y) sd(c(x,y)),A, B), ncol=ncol(A))
# [,1] [,2] [,3]
#[1,] 0.01600819 0.10842068 0.22176990
#[2,] 0.07615823 0.00682358 0.16595089
#[3,] 0.22470439 0.02634680 0.02993183

Writing a shortcut function in R for rm(list=ls())

There are two issues with your function related to environments: both ls() and rm() work in the current environment, which will be the executing frame of the function clr().

In other words, ls() will give you the objects available in the frame of clr(), which is nothing in the case of your function (debug it and see what ls() returns once you are in the execution frame of clr()). Hence you were asking rm() to remove nothing.

Once you fix that, you still need to tell rm() which environment to remove objects from. Here you made another error; the first argument to rm() is ..., which means that you need to fully name arguments to rm() that come after the ... argument. The argument you want is envir not env.

Here is a function which does what you wanted, plus it won't delete itself (!).

clr <- function() {
ENV <- globalenv()
ll <- ls(envir = ENV)
ll <- ll[ll != "clr"]
rm(list = ll, envir = ENV)
}

In use we have

> ls()
[1] "clr" "obj1" "obj2"
> clr()
> ls()
[1] "clr"

Note that as written this won't delete hidden objects (those with a . as the first character of their name). For that you need to use ls(all.names = TRUE).

A final remark, but you don't need the argument x in the definition of clr(); functions with no arguments are fine.

rep function in R function

It's because you are not creating a new variable x in your function, but taking a copy of the x in the enclosing environment, and modifying that. So sum(x) adds 10 elements, the final five of which have the value 100.

To fix, don't assign to a slice of x, assign the result of ceiling to a variable, of any name, even x:

random.sum <- function(n) {
x <- ceiling(10*runif(n))
cat("x:", x[1:n] ,"\n")
return(sum(x))
}

set.seed(3585)
random.sum(10)
## x: 9 4 10 1 9 8 4 1 3 2
## [1] 51

random.sum(5)
## x: 9 6 6 2 2
## [1] 25

Note the difference is 500, the final elements of the global x.

R: apply a function to a list of dataframes and save to workspace

There are a lot of issues in your sample code.

myUser_tw is not reused, you use myUserList_tw instead, probably a typo. I will use myUserList beause using a variable ending with 'tw' wouldn't be consistent, as you're considering those to be tibbles.

Your Mycount function doesn't return x (changed in your edit)

retw and cust are not defined, so I will assume they are strings and you forgot the quotes.

Your loop is not really looping on anything (the i is not used), and the result of lapply is not assigned to anything.

This should work:

dftest_tw <- structure(list(text = c("RT @BitMEXdotcom: A new high: US$500M turnover in the last 24 hours, over 80% of it on $XBTUSD. Congrats to the team and thank you to our u…", 
"RT @Crowd_indicator: Thank you for this nice video, @Nicholas_Merten",
"RT @Crowd_indicator: Review of #Cindicator by DataDash: t.co/D0da3u5y3V"
), Tweet.id = c("896858423521837057", "896858275689398272", "896858135314538497"
), created.date = structure(c(17391, 17391, 17391), class = "Date"),
created.week = c(33, 33, 33)), .Names = c("text", "Tweet.id",
"created.date", "created.week"), row.names = c(NA, -3L), class = c("tbl_df",
"tbl", "data.frame"))

dftest2_tw <- dftest_tw # so we have 2

MyCount <- function(x){
x$retweet <- NA
x$custom <- NA
x$retweet <- grepl("retw", x$text) * 1
x$custom <- (grepl("cust", x$text) & !grepl("retw", x$text)) * 1
x
}

myUserList <- ls(,pattern = "_tw")
for(var in myUserList){
assign(var,MyCount(get(var))) # assign to the variable described by string `var` the result of the function MyCount applied on the value of `var` (itself obtained by `get`)
}


Related Topics



Leave a reply



Submit