Skip to content

Question on "assign(paste.."

3 messages · Kyeongmi Cheon, Rolf Turner, Erik Iverson

#
Try:

	for(t in 4) { # Did you mean ``1:4''?
		nm <- paste("Trt",sep="")
		assign(nm,rmultinom(10,size=5,prob=probTrt[t,]))
		print(rowMeans(get(nm)))
	}

Notes:

	(1) You were missing the ``get(t)''; I introduced ``nm'' to save  
some typing.

	(2) You need the print() inside the for loop, or you won't see any  
results.

	(3) The letter ``t'' is a bad name for an index, since it is the  
name of the
	transpose function.  No *real* harm, but a dubious practice.

	(4) You'd probably be better off using a list, rather than constructing
	    a sequence of names.  As in

	Trt <- list()
	for(i in 1:4) {
		Trt[[i]] <- rmultinom(10,size=5,prob=probTrt[t,])
		print(rowMeans(Trt[[i]])
	}

		cheers,

			Rolf Turner
On 5/03/2008, at 1:44 PM, Kyeongmi Cheon wrote:

            
######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
#
Hello -

I agree with Rolf that a (named) list may be better here.  If you don't 
want to use a for loop, see if the following works using lapply?

probTrt <- list(Trt1 = c(0.064,0.119,0.817),
                 Trt2 = c(0.053,0.125,0.823),
                 Trt3 = c(0.111,0.139,0.750),
                 Trt4 = c(0.351,0.364,0.285))

samp <- lapply(probTrt, function(x) rmultinom(10, size = 5, prob = x))
lapply(samp, rowMeans)

-Erik Iverson
Rolf Turner wrote: