Re: [R] printing a variable name in a for loop
This would do it: v1 - 5 v2 - 6 v3 - 7 vns - paste(v,1:3,sep=) for (i in 1:length(vns)) cat(Hello, vns[i], get(vns[i]), World\n, sep=,) Hello,v1,5,World Hello,v2,6,World Hello,v3,7,World On 24/09/06, Suzi Fei [EMAIL PROTECTED] 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. Thanks, Suzi __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- = David Barron Said Business School University of Oxford Park End Street Oxford OX1 1HP __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] printing a variable name in a for loop
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 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] printing a variable name in a for loop
Example: lst - list(variable1, variable2, variable3) for (kk in seq(along=lst)) { name - names(lst)[kk]; value - lst[[kk]]; cat(Hello,, name, value, , World,) } /Henrik On 9/26/06, Jim Lemon [EMAIL PROTECTED] wrote: 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 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.