Hello R-community: Much of the time I want to use loops to look at graphs,
etc. For example,
I have 25 plots, for which the names are m.1$medoids, m.2$medoids, ...,
m.25$medoids.
I want to index the object number (1:25) as below (just to show concept).
for (i in 1:25){
plot(m.i$medoids)
}
I've tried the following, with negative results
for ...
plot(paste("m.",i,"$medoids",sep=""))
for ..
plot(get(paste("m.",i,"$medoids",sep="")))
for...
plot(eval(paste("m.",i,"$medoids",sep="")))
I'm not very fluent with R, but would like to have the general capacity to
run loops in this fashion. help and archives did not help me, although I'm
sure I've either missed something or did not properly understand the
internals of R.
Any help or alternatives are much welcome; however, note, that alternatives
should be general, because I desire this functionality for other tasks.
Thanks in advance,
Mark Orr
[[alternative HTML version deleted]]
Try:
for( i in 255 ){
plot( get( paste('m.', i, sep='') )$medoids )
}
For future use it may be easier to put m1, m2, ... Into a list and work
with that:
med.list <- list()
for( i in 1:255 ){
med.list[[i]] <- get( paste('m.',i,sep='') )
}
lapply( med.list, function(x) plot(x$medoid) )
Hope this helps,
--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
(801) 408-8111
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Mark Orr
> Sent: Friday, February 22, 2008 1:07 PM
> To: r-help at r-project.org
> Subject: [R] Looping and Pasting
>
> Hello R-community: Much of the time I want to use loops to
> look at graphs, etc. For example,
>
> I have 25 plots, for which the names are m.1$medoids,
> m.2$medoids, ..., m.25$medoids.
> I want to index the object number (1:25) as below (just to
> show concept).
>
> for (i in 1:25){
> plot(m.i$medoids)
> }
>
> I've tried the following, with negative results
>
> for ...
> plot(paste("m.",i,"$medoids",sep=""))
>
> for ..
> plot(get(paste("m.",i,"$medoids",sep="")))
>
> for...
> plot(eval(paste("m.",i,"$medoids",sep="")))
>
>
> I'm not very fluent with R, but would like to have the
> general capacity to run loops in this fashion. help and
> archives did not help me, although I'm sure I've either
> missed something or did not properly understand the internals of R.
>
> Any help or alternatives are much welcome; however, note,
> that alternatives should be general, because I desire this
> functionality for other tasks.
>
> Thanks in advance,
>
> Mark Orr
>
> [[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.
>
See if the following helps you understand what you want.
This won't work:
get("m.1$medoids")
But you can do
get("m.1")$medoids
lst1 <- list(a = 10, b = 20, c = 30) ##create a list
get("lst1") ## gets whole objec
get("lst1$a") ## does not work
get("lst1")$a ## works
get(paste("lst", 1, sep = ""))$a ## same as above with paste
As for alternatives, it could be suggested that you store all your mi's
in a list themselves, and then just use the lapply function to do
whatever processing you want. You then would not have to mess with
'get', 'paste', and for loops.
Best,
Erik Iverson
Mark Orr wrote:> Hello R-community: Much of the time I want to use loops to look at graphs,
> etc. For example,
>
> I have 25 plots, for which the names are m.1$medoids, m.2$medoids, ...,
> m.25$medoids.
> I want to index the object number (1:25) as below (just to show concept).
>
> for (i in 1:25){
> plot(m.i$medoids)
> }
>
> I've tried the following, with negative results
>
> for ...
> plot(paste("m.",i,"$medoids",sep=""))
>
> for ..
> plot(get(paste("m.",i,"$medoids",sep="")))
>
> for...
> plot(eval(paste("m.",i,"$medoids",sep="")))
>
>
> I'm not very fluent with R, but would like to have the general capacity
to
> run loops in this fashion. help and archives did not help me, although
I'm
> sure I've either missed something or did not properly understand the
> internals of R.
>
> Any help or alternatives are much welcome; however, note, that alternatives
> should be general, because I desire this functionality for other tasks.
>
> Thanks in advance,
>
> Mark Orr
>
> [[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.
Possibly Parallel Threads
- [cluster package question] What is the "sum of the dissimilarities" in the pam command ?
- Getting individual co-ordinate points in k medoids cluster
- passing known medoids to clara() in the cluster package
- pam() clustering for large data sets
- bug (?!) in "pam()" clustering from fpc package ?