[R] Paste a character to an object

Tim Clark mudiver1200 at yahoo.com
Sun Oct 4 05:14:53 CEST 2009


David,

Thanks, that helps me in making an example of what I am trying to do.  Given the following example, I would like to run through a for loop and obtain a vector of the data only for the 100, 75, and 50 percent values.  Is there a way to get this to work, either using paste as in the example below or some other method?

homerange <- list()
homerange[[1]] <- "test"
homerange[[1]]$polygons <- "test2"
homerange[[1]]$polygons$`100` <- rnorm(20,10,1)
homerange[[1]]$polygons$`90` <- rnorm(20,10,1)
homerange[[1]]$polygons$`75` <- rnorm(20,10,1)
homerange[[1]]$polygons$`50` <- rnorm(20,10,1)

xx<-c()
percent<-c("100","75","50")
for (i in 1:length(percent))
{
x<-paste(homerange[[1]]$polygons$,percent[i]) #This does not work!!!
xx<-rbind(x,xx)
}

The x<-paste(...) in this function does not work, and that is what I am stuck on.  The result should be a vector the values for the "100","75",and "50" levels, but not the "90" level.

Aloha,

Tim




Tim Clark
Department of Zoology 
University of Hawaii


--- On Sat, 10/3/09, David Winsemius <dwinsemius at comcast.net> wrote:

> From: David Winsemius <dwinsemius at comcast.net>
> Subject: Re: [R] Paste a character to an object
> To: "Tim Clark" <mudiver1200 at yahoo.com>
> Cc: r-help at r-project.org
> Date: Saturday, October 3, 2009, 4:45 PM
> 
> On Oct 3, 2009, at 10:26 PM, Tim Clark wrote:
> 
> > Dear List,
> > 
> > I can't seem to get a simple paste function to work
> like I need.  I have an object I need to call but it
> ends in a character string.  The object is a list of
> home range values for a range of percent isopleths.  I
> need to loop through a vector of percent values, so I need
> to paste the percent as a character on the end of the object
> variable.  I have no idea why the percent is in
> character form, and I can't use a simple index value
> (homerange[[1]]$polygons[100]) because there are a variable
> number of isopleths that are calculated and [100] will not
> always correspond to "100".  So I am stuck.
> > 
> > What I want is:
> > 
> > homerange[[1]]$polygons$"100"
> > 
> > What I need is something like the following, but that
> works:
> > 
> > percent<-c("100","75","50")
> > p=1
> > paste(homerange[[1]]$polygons$,percent[p],sep="")
> 
> Not a reproducible example, but here is some code that
> shows that it is possible to construct names that would
> otherwise be invalid due to having numerals as a first
> character by using back-quotes:
> 
> > percent<-c("100","75","50")
> > p=1
> > paste(homerange[[1]]$polygons$,percent[p],sep="")
> Error: syntax error
> > homerange <- list()
> > homerange[[1]] <- "test"
> > homerange[[1]]$polygons <- "test2"
> Warning message:
> In homerange[[1]]$polygons <- "test2" : Coercing LHS to
> a list
> > homerange
> [[1]]
> [[1]][[1]]
> [1] "test"
> 
> [[1]]$polygons
> [1] "test2"
> 
> 
> > homerange[[1]]$polygons$`100` <- percent[1]
> Warning message:
> In homerange[[1]]$polygons$`100` <- percent[1] :
> Coercing LHS to a list
> > homerange[[1]]$polygons$`100`
> [1] "100"
> 
> --David Winsemius
> 
> 
> > 
> > Thanks for the help,
> > 
> > Tim
> > 
> > 
> > 
> > Tim Clark
> > Department of Zoology
> > University of Hawaii
> > 
> > ______________________________________________
> > 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.
> 
> 







More information about the R-help mailing list