[R] printing a variable name in a for loop

Jim Lemon jim at bitwrit.com.au
Wed Sep 27 02:14:05 CEST 2006


Suzi Fei wrote:
> Hello,
> 
> How do you print a variable name in a for loop?
> 
> I'm trying to construct a csv file that looks like this:
> 
> 
> 	Hello, variable1, value_of_variable1, World,
> 	Hello, variable2, value_of_variable2, World,
> 	Hello, variable3, value_of_variable3, World,
> 
> 
> Using this:
> 
> 	for (variable in list(variable1, variable2, variable3)){
> 
> 		cat("Hello,", ???variable???, variable, ", World,")
> 	}
> 
> This works fine if I'm trying to print the VALUE of variable, but I want to
> print the NAME of variable as well.
> 
This is a teetering heap of assumptions, but is this what you wanted?

Suzi<-1
HiYa<-function(x) {
  cat("Hello",deparse(substitute(x)),x,"World\n",sep=", ")
}
HiYa(Suzi)

Jim



More information about the R-help mailing list