cannot coerce class '"expression"' to a data.frame

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

cannot coerce class '"expression"' to a data.frame

tring
Dear friends - I have some old data of chemical results and want to annotate
the points with chemical formula. If I just do

ID <- c("HCl 7","HCl 5.25","HCl 3.5","HCL 7 no Na","HCl 3.5  No Na",

"HSO4 7 no  Na",

        "HNO3 7 No Na")

I can make a data.frame like

DD <- data.frame(x=1:7,ID)

 

and plotting works fine.

But if I try to format sulfuric acid and nitric acid like

ID <- c("HCl 7","HCl 5.25","HCl 3.5","HCL 7 no Na","HCl 3.5  No Na",

        expression(paste(HSO[4]," 7 no  Na")),

        expression(paste(HNO[3]," 7 No Na")))

 

DD <- data.frame(x=1:7,ID)

Elicits an error

Error in as.data.frame.default(x[[i]], optional = TRUE) :

  cannot coerce class '"expression"' to a data.frame

and plotting is prohibited - so how is this bypassed?

 

I'm on Windows 10,

R version 3.6.1 (2019-07-05)

 

All best wishes

Troels Ring, MD

Aalborg, Denmark


This email has been scanned by BullGuard antivirus protection.
For more info visit www.bullguard.com
<http://www.bullguard.com/tracking.aspx?affiliate=bullguard&buyaffiliate=smt
p&url=/>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: cannot coerce class '"expression"' to a data.frame

R help mailing list-2
Try using the I() function in the call to data.frame.  E.g.,

> d <- data.frame(X=1:3, Y=1:3, Substance=I(expression(H[2]*O, H[2] * O[2],
H*S*O[4])))
> with(d, {plot(X,Y,type="n",xlim=c(0,4)); text(X, Y, Substance)})

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Sat, Mar 21, 2020 at 1:45 AM Troels Ring <[hidden email]> wrote:

> Dear friends - I have some old data of chemical results and want to
> annotate
> the points with chemical formula. If I just do
>
> ID <- c("HCl 7","HCl 5.25","HCl 3.5","HCL 7 no Na","HCl 3.5  No Na",
>
> "HSO4 7 no  Na",
>
>         "HNO3 7 No Na")
>
> I can make a data.frame like
>
> DD <- data.frame(x=1:7,ID)
>
>
>
> and plotting works fine.
>
> But if I try to format sulfuric acid and nitric acid like
>
> ID <- c("HCl 7","HCl 5.25","HCl 3.5","HCL 7 no Na","HCl 3.5  No Na",
>
>         expression(paste(HSO[4]," 7 no  Na")),
>
>         expression(paste(HNO[3]," 7 No Na")))
>
>
>
> DD <- data.frame(x=1:7,ID)
>
> Elicits an error
>
> Error in as.data.frame.default(x[[i]], optional = TRUE) :
>
>   cannot coerce class '"expression"' to a data.frame
>
> and plotting is prohibited - so how is this bypassed?
>
>
>
> I'm on Windows 10,
>
> R version 3.6.1 (2019-07-05)
>
>
>
> All best wishes
>
> Troels Ring, MD
>
> Aalborg, Denmark
>
>
> This email has been scanned by BullGuard antivirus protection.
> For more info visit www.bullguard.com
> <
> http://www.bullguard.com/tracking.aspx?affiliate=bullguard&buyaffiliate=smt
> p&url=/
> <http://www.bullguard.com/tracking.aspx?affiliate=bullguard&buyaffiliate=smtp&url=/>>
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: cannot coerce class '"expression"' to a data.frame

tring
In reply to this post by tring
Thanks a lot - this does indeed do the trick!

-----Oprindelig meddelelse-----
Fra: peter dalgaard <[hidden email]>
Sendt: 21. marts 2020 18:08
Til: Troels Ring <[hidden email]>
Emne: Re: [R] cannot coerce class '"expression"' to a data.frame

Oops, disregard previous...

I'm no ggplot expert, but google "ggplot plotmath" suggests that it wants unparsed expressions, so:

dd <- data.frame(x=1:7, ID = I(ID), ID2 = sapply(ID, deparse))
ggplot(data=dd,aes(x=x,y=x,label=ID2)) + geom_point(color="blue",size=4) + geom_label(parse=TRUE)

seems to do something in the right direction.

-p

> On 21 Mar 2020, at 13:44 , Troels Ring <[hidden email]> wrote:
>
> Thanks a lot - actually I was on my way to ggplot2 - and still gets
> struck - sorry 😊
>
> ID <- c("HCl 7","HCl 5.25","HCl 3.5","HCL 7 no Na","HCl 3.5  No Na",
>        expression(paste(H[2]*SO[4]," 7 no  Na")),
>        expression(paste(HNO[3]," 7 No Na")))
>
> dd <- data.frame(x=1:7, ID = I(ID))
>
> library(ggplot2)
>
> ggplot(data=dd,aes(x=x,y=x,label=ID))+geom_point(color="blue",size=4)
> # Error: Aesthetics must be valid data columns. Problematic aesthetic(s): label = ID.
> # Did you mistype the name of a data column or forget to add stat()?
>
> -----Oprindelig meddelelse-----
> Fra: peter dalgaard <[hidden email]>
> Sendt: 21. marts 2020 11:14
> Til: Troels Ring <[hidden email]>
> Emne: Re: [R] cannot coerce class '"expression"' to a data.frame
>
> This seems to work:
>
> ID <- c("HCl 7","HCl 5.25","HCl 3.5","HCL 7 no Na","HCl 3.5  No Na",
>        expression(paste(HSO[4]," 7 no  Na")),
>        expression(paste(HNO[3]," 7 No Na")))
>
> ## Actually H[2]*SO[4], no?
>
> dd <- data.frame(x=1:7, ID = I(ID))
> plot(dd$x, dd$x, typ="n")
> text(dd$x, dd$x, dd$ID)
>
>
>> On 21 Mar 2020, at 09:44 , Troels Ring <[hidden email]> wrote:
>>
>> Dear friends - I have some old data of chemical results and want to
>> annotate the points with chemical formula. If I just do
>>
>> ID <- c("HCl 7","HCl 5.25","HCl 3.5","HCL 7 no Na","HCl 3.5  No Na",
>>
>> "HSO4 7 no  Na",
>>
>>       "HNO3 7 No Na")
>>
>> I can make a data.frame like
>>
>> DD <- data.frame(x=1:7,ID)
>>
>>
>>
>> and plotting works fine.
>>
>> But if I try to format sulfuric acid and nitric acid like
>>
>> ID <- c("HCl 7","HCl 5.25","HCl 3.5","HCL 7 no Na","HCl 3.5  No Na",
>>
>>       expression(paste(HSO[4]," 7 no  Na")),
>>
>>       expression(paste(HNO[3]," 7 No Na")))
>>
>>
>>
>> DD <- data.frame(x=1:7,ID)
>>
>> Elicits an error
>>
>> Error in as.data.frame.default(x[[i]], optional = TRUE) :
>>
>> cannot coerce class '"expression"' to a data.frame
>>
>> and plotting is prohibited - so how is this bypassed?
>>
>>
>>
>> I'm on Windows 10,
>>
>> R version 3.6.1 (2019-07-05)
>>
>>
>>
>> All best wishes
>>
>> Troels Ring, MD
>>
>> Aalborg, Denmark
>>
>>
>> This email has been scanned by BullGuard antivirus protection.
>> For more info visit www.bullguard.com
>> <http://www.bullguard.com/tracking.aspx?affiliate=bullguard&buyaffili
>> a
>> te=smt
>> p&url=/>
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School Solbjerg Plads 3,
> 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Office: A 4.23
> Email: [hidden email]  Priv: [hidden email]
>
>
>
>
>
>
>
>
>
>
>
> This email has been scanned by BullGuard antivirus protection.
> For more info visit www.bullguard.com
>
>

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: [hidden email]  Priv: [hidden email]











This email has been scanned by BullGuard antivirus protection.
For more info visit www.bullguard.com

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.