Re: [R] How to paste a vector of expressions and a character vector?

From: David Winsemius <dwinsemius_at_comcast.net>
Date: Fri, 01 Apr 2011 14:40:20 -0400

On Apr 1, 2011, at 2:34 PM, David Winsemius wrote:

>
> On Apr 1, 2011, at 1:56 PM, Marius Hofert wrote:
>
>> Dear David,
>>
>> thanks for your reply. The paste() meant to paste the vectors vec1
>> and vec2 together, so main should be a vector of length 3 of the form
>> "a 1", "b 2", "c 3" # with b being tilde(b)
>> However, with c() it is a vector of length 6:
>> expression("a", tilde(b), "c", "1", "2", "3")
>
> lls <-c("a", "tilde(b)","c")
nns <- 1:3

#Even easier would be:

 > mains3 <- parse(text=paste(lls,"~",nns,sep=""))  > mains3
expression(a~1, tilde(b)~2, c~3)
attr(,"srcfile")
<text>
attr(,"wholeSrcref")
a~1
tilde(b)~2
c~3

> mains <- bquote(.(parse(text=paste(lls,"~",nns,sep=""))) )
> mains
>
> #expression(a~1, tilde(b)~2, c~3)
> #attr(,"srcfile")
> <text>
> attr(,"wholeSrcref")
> a~1
> tilde(b)~2
> c~3
> #-----
>
> > mains[2]
> #expression(tilde(b)~2)
>
>
> plot(0,0, main=mains[2])
>
>
> This would also work:
>
> > mains2 <- as.expression(parse(text=paste(lls,"~",nns,sep="")))
> > mains2
> expression(a~1, tilde(b)~2, c~3)
> attr(,"srcfile")
> <text>
> attr(,"wholeSrcref")
> a~1
> tilde(b)~2
> c~3
>
> I try to avoid using spaces in expressions and instead use "~"'s
> because I don't really understand how spaces get parsed in
> expression operations, and I do know how plotmath operations work
> with "~".
>
>>
>> Do you know a solution for that?
>>
>> Cheers,
>>
>> Marius
>>
>> On 2011-04-01, at 19:22 , David Winsemius wrote:
>>
>>>
>>> On Apr 1, 2011, at 12:32 PM, Marius Hofert wrote:
>>>
>>>> Dear expeRts,
>>>>
>>>> I know I can't paste expressions in the normal way, but I just
>>>> couldn't figure out
>>>> how to get the following (I want to paste a character vector to
>>>> an expression vector)
>>>> right with bquote() or substitute.
>>>>
>>>> vec1 <- c("a", expression(tilde(b)), "c")
>>>> vec2 <- c("1", "2", "3")
>>>> main <- as.expression(paste(vec1, vec2))
>>>> plot(0,0, main=main[2])
>>>
>>> Do not use `paste` ... it coerces your expression to a character
>>> value ... use `c` instead:
>>>
>>>> main <- as.expression(c(vec1, vec2))
>>>> plot(1,1, main=main[2])
>>>
>>> And then, even the as.expression is superfluous:
>>>
>>>> main <- c(vec1, vec2)
>>>> plot(1,1, main=main[2])
>>>
>>> --
>>>
>>> David Winsemius, MD
>>> West Hartford, CT
>>>
>>
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> 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
West Hartford, CT



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. Received on Fri 01 Apr 2011 - 18:42:48 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 01 Apr 2011 - 19:50:26 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive