I am trying to rename global variables with a for loop.
I do not have a reproducible example because this question relates to something very broad.
The first issue is that I am dealing with such large data sets that I must use data.tables. I want to rename (with a for loop) all data.tables in my global environment.
The first problem is how do I rename global environment variables.
Second, how do I rename global variables of a certain class type?
Mind, I want to do this in a for loop so that it can rename lists of variable length.
R does not have the concept of re-naming, but you can bind an object to a new
name and remove the old name. Since data.table objects use references, this
won't result in copying the underlying data, the way other assignments would.
# get the names of the variables in the global environment
globalVariables <- ls(envir=.GlobalEnv)
for(.name in globalVariables){
x <- get(.name,envir=.GlobalEnv)
if(inherits(x,'data.table')){
# obviously you want a better newName than 'foo'
newName <- 'foo'
# bind the value x to the new name in the global environment
assign(newName,x,envir=.GlobalEnv)
# delete the binding to the old name in the global environment
rm(list=.name,envir=.GlobalEnv)
}
}
Related
Sometimes data files come in .rdata objects. These are annoying compared to .rds files because the objects have predefined names. In my case, I want to rename the object automatically and get rid of the wrongly named version. Simple somewhat contrived example:
#make a new iris with a bad name
badnameiris = iris
#save it to a file
save(badnameiris, file = "iris.rdata")
#rename badname version from global envir
rm(badnameiris)
#read iris from file
irisname = load("iris.rdata")
#this variable is not iris, but the name of the variable it was assigned to
irisname
[1] "badnameiris"
#it's to use the right name with get()
goodnameiris = get(irisname)
#but harder to get rid of the wrong one with rm()
rm(irisname)
The last line does not work as intended because it requires a bare name as input and it gets a character vector. I realize one can actually use the list argument in rm(), but suppose one could not.
How does one in general convert from character to unquoted for these purposes?
I tried the rlang functions, but these are for non-standard evaluation as used in tidyverse context. I tried as.name(), as suggested here. Does not work either. Most questions I could find asking this question relate to tidyverse, but I'm trying to do base R context.
(An alternative solution above is to make a function that utilizes the destruction of the local environment to remove the unwanted copy of the object.)
Just use do.call:
x <- 1
s <- "x"
do.call(rm, list(s))
ls()
#[1] "s"
Or compute on the language:
eval(bquote(rm(.(s))))
I am new to R and here. Sorry If I am complicated. I created a function by trial name. There are some functions from my own function below.
write.table(pars, "C:\\Parameters\\pars_(variable name).txt")
cov12lag1_(variable name) <<- acvf(half, 1)
Let's say I have two different data. Data names are jan2005 and jan2006
When I write function trial(jan2005) I want to get pars_jan2005.txt and also in my environment I want to see cov12lag1_jan2005 automatically.
Also, again when I want to write trial(jan2006), again I want to see pars_jan2006.txt and cov12lag1_jan2006 automatically.
So these were only examples, my aim is when I write a variable in a function, I want all desired parts to be changed with variable name.
The randomNames package in R can be used to generate random first and last names. There is an environment called randomNamesData that is loaded with the package and randomNamesData contains
> randomNamesData
<environment: 0x11db12078>
Similar output for str(randomNamesData) and dput(randomNamesData).
if you examine this randomNamesData with View you get the following:
> View(randomNamesData)
The objects in each of these objects (such as first_names_e1_g0) is of type array.
Is it possible to modify the randomNamesData environment to add more names? If it is possible, how to add/remove names to it?
I'm not clear on the exact changes you want to make, but since environments behave like lists, it should be possible to pull the data of interest into a temporary variable, modify that variable, and then replace the original:
tmp <- randomNamesData$first_names_e1_g0
[make changes to tmp]
randomNamesData$first_names_e1_g0 <- tmp
I'm having a problem with the below function:
ab<-matrix(c(1:20),nrow=4)
rownames(ab)<-c("a","b","c","d")
cd<-c("a","c")
test<-function(x,y,ID_Tag){
for(i in y) {
M_scaled<-t(scale(t(x),center=T))
a<-quantile(M_scaled[match(i,rownames(x)),])
assign(paste0("Probes_",ID_Tag,"_quan_",i),a)
}
}
test(ab,cd,"C1")
x is the dataframe/matrix
y is the string I need to search for in rownames(x)
ID_Tag is is the number I use to distinguish my samples from each other.
The function is running, but no output is generated into strings afterwards.
Hope somebody can help me
When you use assign within a function it will make the assignment to a variable that is accessible within that function only (i.e. it's like using <-). To get around this, you need to specify the envir argument in assign to be either the global environment globalenv() or the parent frame of the function. So try changing your assign statement to
assign(..., envir = parent.frame())
or
assign(..., envir = globalenv())
depending on what you want exactly (in the example you provided they are equivalent). Have a look at ?parent.frame for more info on these. Another possibility is to specify the pos argument in assign, check ?assign.
As an aside, assigning global objects from within a function can lead to various problems in general. I find it better practice in your example to return a list of objects created in the for loop rather than use assign.
I've been defining some variables with the "<-", like
elect<- copPob12$nivelaprob[copPob12$dispelect==5]
I can see their numerical values on the global environment,
but I want to see how I defined them, to be sure about the function I used, because they are subsets within subsets, I can find them in the "History" tab, but that takes too long,
any function that can retrieve the way I defined the variable on the console?
thanks a lot
As I see the problem, may be you are looking for this:
elect <<- copPob12$nivelaprob[copPob12$dispelect==5]
or you can write
elect <- copPob12$nivelaprob[copPob12$dispelect==5]
assign("elect", elect , envir = .GlobalEnv)
here it changes environment as global so it works within function also