Skip to content

variable name question

4 messages · Deepankar Basu, R. Michael Weylandt, Rolf Turner

#
"This is surely an easy question but somehow I am not being able to get it."

get() is the key -- it takes a string and returns the object with that
string as its name. Assign() goes the other way

Try this:

for (i in 1990:2009) {
    varName = paste("pci", i, collapse = "")
    assign(varName, log(get(varName))
}

That said, the standard advice is that its usually more R-ish to keep
all your data in a list, data frame or, for this case, a matrix.

Michael
On Sun, Oct 9, 2011 at 11:46 AM, Deepankar Basu <basu.15 at gmail.com> wrote:
#
On 10/10/11 04:53, R. Michael Weylandt wrote:
<SNIP>
<SNIP>

I believe that ``sep= " '' is needed here rather than collapse.

Try:
         paste("junk",42,collapse="")

You get

     [1] "junk 42"

with a space in it.  Here paste is using the default value of sep, 
namely " ",
and is not using collapse at all, since there is nothing to collapse (the
value is a scalar to start with).

Whereas

         paste("junk",42,sep="")

gives

     [1] "junk42"

which is what is wanted.

     cheers,

         Rolf Turner