[R] Paste a character to an object

David Winsemius dwinsemius at comcast.net
Sun Oct 4 04:45:31 CEST 2009


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