Skip to content
Prev 163961 / 398506 Next

assigning a new variable to multiple data frames

Viewing ?assign we see that the first two arguments to assign are required
but in the example shown in the question there is only one argument.

Also ?assign says the first argument is "a variable name, given as a
character string"
which is not the case in the code in the question.

Also as a matter of course its not a good idea to use c as a variable name even
though R can usually determine that you did not mean the function c (although
sometimes it can't).

C <- matrix(1:100, 10)
cc <- matrix(1:50, 10)

for(m in c("C", "cc")) assign(m, cbind(get(m), new = 9))

Depending on your intention you may prefer to have C and cc be list
components.  That would significantly simplify iteration over them. In
that case c could be a name since names of list components cannot
be confused with functions:

L <- list(c = matrix(1:100, 10), cc = matrix(1:50, 10))
L <- lapply(L, cbind, new = 9)
On Sat, Dec 6, 2008 at 4:09 PM, Georg Ehret <georgehret at gmail.com> wrote: