[R] Paste a character to an object
David Winsemius
dwinsemius at comcast.net
Sun Oct 4 05:43:18 CEST 2009
On Oct 3, 2009, at 11:14 PM, Tim Clark wrote:
> 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!!!
^?^
And why _would_ you expect an expression ending in a "$" to be
acceptable to the parser? You did not put quotes around it so the
interpreter tried to evaluate it.
You are probably looking for the capabilities of the functions get and
assign which take string variable and either get the object named by a
sstring or assign a vlaue to an object so named.
But why are you intent in causing yourself all this pain? (Not to
mention asking questions I cannot answer.) Working with expressions
involving backquotes is a recipe for hair-pulling and frustration for
us normal mortals. Why not call your lists "p100", "p90", "p75",
"p50"? Then everything is simple:
> xx<-c()
> percent<-c(100, 75, 50)
> for (i in c("p100", "p75", "p50") )
+ {
+ x<-homerange[[1]]$polygons[[i]] ; xx<-rbind(x,xx) # could have
simplified this
+ }
> xx
[,1] [,2] [,3] [,4] [,5] [,6] [,
7] [,8] [,9]
x 9.660935 10.46526 10.75813 8.866064 9.967950 9.987941 10.757160
10.180826 9.992162
x 11.674645 10.51753 10.88061 10.515120 9.440838 11.460845 12.033612
9.318392 9.592026
x 10.057021 10.14339 10.29757 9.164233 8.977280 9.733971 9.965002
9.693649 9.430043
[,10] [,11] [,12] [,13] [,14] [,15] [,
16] [,17] [,18]
x 11.78904 9.437353 11.910747 10.996167 11.631264 9.386944 9.602160
10.498921 9.09349
x 9.11036 9.546378 11.030323 9.715164 9.500268 11.762440
9.101104 9.610251 10.56210
x 9.62574 12.738020 9.146863 10.497626 10.485520 11.644503 10.303581
11.340263 11.34873
[,19] [,20]
x 10.146955 9.640136
x 9.334912 10.101603
x 8.710609 11.265633
>
>
> 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 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.
>>
>>
>
>
>
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
More information about the R-help
mailing list