# Re: [R] Question on "assign(paste.."

From: Erik Iverson <iverson_at_biostat.wisc.edu>
Date: Tue, 04 Mar 2008 20:36:39 -0600

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:
> 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:
>

```>> Hello,
>> I'm having trouble in using "assign(paste ..." command . I could
>> create
>> several dataframes following trinomial distribution using it but it
>> could
>> not be used to check their row means of the created dataframe.
>>
>> For example, the following works:
>>
>> probTrt=matrix(0,4,3);
>> probTrt;
>> #malf, death, normal
>> probTrt[1,]=c(0.064,0.119,0.817);#for Trt 1
>> probTrt[2,]=c(0.053,0.125,0.823);#for Trt 2
>> probTrt[3,]=c(0.111,0.139,0.750);#for Trt 3
>> probTrt[4,]=c(0.351,0.364,0.285);#for Trt 4
>> for (t in 4){
>>         assign(paste("Trt",t,sep=""),rmultinom(10, size = 5,
>> prob=probTrt[t,]));
>>     }
>>
>>
>>
>> But the following does not work.
>> for (t in 4){
>>         assign(paste("Trt",t,sep=""),rmultinom(10, size = 5,
>> prob=probTrt[t,]));
>>         rowMeans(paste("Trt",t,sep=""));
>>     }
>>
>>
>> How can I use it in functions like rowMeans so that I don't have to
>> type all
>> the object names? Thank you.
>>
>> Kyeongmi
>>
>> 	[[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help_at_r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> guide.html
>> and provide commented, minimal, self-contained, reproducible code.
```

>
>
> ######################################################################
> Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help