Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting strings recognized as variable names in R

Related: Strings as variable references in R
Possibly related: Concatenate expressions to subset a dataframe


I've simplified the question per the comment request. Here goes with some example data.

dat <- data.frame(num=1:10,sq=(1:10)^2,cu=(1:10)^3) set1 <- subset(dat,num>5) set2 <- subset(dat,num<=5) 

Now, I'd like to make a bubble plot from these. I have a more complicated data set with 3+ colors and complicated subsets, but I do something like this:

symbols(set1$sq,set1$cu,circles=set1$num,bg="red") symbols(set2$sq,set2$cu,circles=set2$num,bg="blue",add=T) 

I'd like to do a for loop like this:

colors <- c("red","blue") sets <- c("set1","set2") vars <- c("sq","cu","num")  for (i in 1:length(sets)) {    symbols(sets[[i]][,sq],sets[[i]][,cu],circles=sets[[i]][,num],    bg=colors[[i]],add=T) }     

I know you can have a variable evaluated to specify the column (like var="cu"; set1[,var]; I want to know how to get a variable to specify the data.frame itself (and another to evaluate the column).


Update: Ran across this post on r-bloggers which has this example:

x <- 42 eval(parse(text = "x")) [1] 42 

I'm able to do something like this now:

eval(parse(text=paste(set[[1]],"$",var1,sep=""))) 

In fiddling with this, I'm finding it interesting that the following are not equivalent:

vars <- data.frame("var1","var2") eval(parse(text=paste(set[[1]],"$",var1,sep=""))) eval(parse(text=paste(set[[1]],"[,vars[[1]]]",sep=""))) 

I actually have to do this:

eval(parse(text=paste(set[[1]],"[,as.character(vars[[1]])]",sep=""))) 

Update2: The above works to output values... but not in trying to plot. I can't do:

for (i in 1:length(set)) { symbols(eval(parse(text=paste(set[[i]],"$",var1,sep=""))),        eval(parse(text=paste(set[[i]],"$",var2,sep=""))),        circles=paste(set[[i]],".","circles",sep=""),        fg="white",bg=colors[[i]],add=T) } 

I get invalid symbol coordinates. I checked the class of set[[1]] and it's a factor. If I do is.numeric(as.numeric(set[[1]])) I get TRUE. Even if I add that above prior to the eval statement, I still get the error. Oddly, though, I can do this:

set.xvars <- as.numeric(eval(parse(text=paste(set[[i]],"$",var1,sep="")))) set.yvars <- as.numeric(eval(parse(text=paste(set[[i]],"$",var2,sep="")))) symbols(xvars,yvars,circles=data$var3) 

Why different behavior when stored as a variable vs. executed within the symbol function?

like image 512
Hendy Avatar asked Jan 29 '12 21:01

Hendy


People also ask

How can you use string as a variable name?

String Into Variable Name in Python Using the vars() Function. Instead of using the locals() and the globals() function to convert a string to a variable name in python, we can also use the vars() function. The vars() function, when executed in the global scope, behaves just like the globals() function.

How do you make a string variable in R?

A string in R can be created using single quotes or double quotes. . Both have class “character” but the empty string has length equal to 1 while the empty character vector has length equal to zero. elements.

How to assign character string to variable name in R?

We can assign character string to variable name by using assign () function. We simply have to pass the name of the variable and the value to the function. value is the variable. We can also create a vector with a group of variables and assign a single variable name. This function allows you to call any R function.

How do I parse a list of strings in R?

I am using R to parse a list of strings in the form: First, I extract the variable name and value from the original string and convert the value to numeric class. Then, I would like to assign the value to a variable with the same name as the parameter_name string.

How to call a function from a variable in R?

We simply have to pass the name of the variable and the value to the function. value is the variable. We can also create a vector with a group of variables and assign a single variable name. This function allows you to call any R function. It allows using a list to hold the arguments of the function along with passing of single arguments.

How do I turn a character string into a variable name?

Let’s dive right in! Example 1 shows how to turn a character string to a variable name using the assign function. Have a look at the following R code: Within the assign function, we defined our character string (i.e. “my_string_1”) as well as the values we want to store in the new variable (i.e. a numeric range from 1 to 5).


1 Answers

You found one answer, i.e. eval(parse()) . You can also investigate do.call() which is often simpler to implement. Keep in mind the useful as.name() tool as well, for converting strings to variable names.

like image 151
Carl Witthoft Avatar answered Nov 16 '22 00:11

Carl Witthoft