[R] Question on "assign(paste.."
Rolf Turner
r.turner at auckland.ac.nz
Wed Mar 5 03:24:13 CET 2008
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
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.
######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
More information about the R-help
mailing list