Shawn Way
2018-May-30 18:14 UTC
[R] Evaluation failure of IAPWS95 functions in a rowwise manner (tidyverse style)
I'm trying to use the IAPWS95 package with the tidyverse packages. For some
reason, the function is not outputting the correct rho.
A minimal example with results is below. I've also included the definition
of the DTp function from the IAPWS95 library.
===================================library(IAPWS95)
library(tidyverse)
initial <- data.frame(T=c(279,294),p=c(0.46,0.46))
initial2 <- initial %>%
mutate(rho=DTp(T,p))
> DTp
function (T, p)
{
y <- 0
icode <- 0
res <- .Fortran("DTp", as.double(T), as.double(p),
as.double(y),
as.integer(icode))
options(digits = 9)
if (res[[4]] != 0) {
error <- as.character(errorCodes[which(errorCodes[, 1] ==
res[[4]]), 2])
print(error)
}
print(res[[3]])
}
<bytecode: 0x0000000006f520e0>
<environment: namespace:IAPWS95>
Results:
> initial2
T p rho
1 279 0.46 1000.12283
2 294 0.46 1000.12283
What the results should be:
> initial2
T p rho
1 279 0.46 1000.12283
2 294 0.46 998.19167
I think something is evaluating incorrectly, but I don't know enough about
NSE vs SE to figure this out.
Any thoughts to what could be the reason why the second row has the same rho as
the first? When I run the function DTp individually, I get the right results.
Shawn Way
???
Ista Zahn
2018-May-30 19:28 UTC
[R] Evaluation failure of IAPWS95 functions in a rowwise manner (tidyverse style)
Hi Shawn, I don't think it has anything to do with the tidyverse. If you keep simplifying your example you'll get all the way down to> DTp(T=c(279,294),p=c(0.46,0.46))[1] 1000.12283 --Ista On Wed, May 30, 2018 at 2:14 PM, Shawn Way <SWay at meco.com> wrote:> I'm trying to use the IAPWS95 package with the tidyverse packages. For some reason, the function is not outputting the correct rho. > > A minimal example with results is below. I've also included the definition of the DTp function from the IAPWS95 library. > ===================================> library(IAPWS95) > library(tidyverse) > > initial <- data.frame(T=c(279,294),p=c(0.46,0.46)) > initial2 <- initial %>% > mutate(rho=DTp(T,p)) > >> DTp > function (T, p) > { > y <- 0 > icode <- 0 > res <- .Fortran("DTp", as.double(T), as.double(p), as.double(y), > as.integer(icode)) > options(digits = 9) > if (res[[4]] != 0) { > error <- as.character(errorCodes[which(errorCodes[, 1] => res[[4]]), 2]) > print(error) > } > print(res[[3]]) > } > <bytecode: 0x0000000006f520e0> > <environment: namespace:IAPWS95> > > Results: > >> initial2 > T p rho > 1 279 0.46 1000.12283 > 2 294 0.46 1000.12283 > > > What the results should be: > >> initial2 > T p rho > 1 279 0.46 1000.12283 > 2 294 0.46 998.19167 > > I think something is evaluating incorrectly, but I don't know enough about NSE vs SE to figure this out. > > Any thoughts to what could be the reason why the second row has the same rho as the first? When I run the function DTp individually, I get the right results. > > > Shawn Way > > > > ______________________________________________ > R-help at r-project.org 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.
Shawn Way
2018-May-30 19:50 UTC
[R] Evaluation failure of IAPWS95 functions in a rowwise manner (tidyverse style)
The printed output is an issue (I've emailed the maintainer about it) however, I think I figured it out. It seems that the function is not vectorized, making me need to do the following: mutate(SG=map2(T,p,function(x,y) DTp(x,y)) This was able to get the correct results. Like usual, the error was between the head and the keyboard. Thanks! Shawn Way -----Original Message----- From: Ista Zahn <istazahn at gmail.com> Sent: Wednesday, May 30, 2018 2:28 PM To: Shawn Way <SWay at meco.com> Cc: r-help at r-project.org Subject: Re: [R] Evaluation failure of IAPWS95 functions in a rowwise manner (tidyverse style) Hi Shawn, I don't think it has anything to do with the tidyverse. If you keep simplifying your example you'll get all the way down to> DTp(T=c(279,294),p=c(0.46,0.46))[1] 1000.12283 --Ista On Wed, May 30, 2018 at 2:14 PM, Shawn Way <SWay at meco.com> wrote:> I'm trying to use the IAPWS95 package with the tidyverse packages. For some reason, the function is not outputting the correct rho. > > A minimal example with results is below. I've also included the definition of the DTp function from the IAPWS95 library. > ===================================> library(IAPWS95) > library(tidyverse) > > initial <- data.frame(T=c(279,294),p=c(0.46,0.46)) > initial2 <- initial %>% > mutate(rho=DTp(T,p)) > >> DTp > function (T, p) > { > y <- 0 > icode <- 0 > res <- .Fortran("DTp", as.double(T), as.double(p), as.double(y), > as.integer(icode)) > options(digits = 9) > if (res[[4]] != 0) { > error <- as.character(errorCodes[which(errorCodes[, 1] => res[[4]]), 2]) > print(error) > } > print(res[[3]]) > } > <bytecode: 0x0000000006f520e0> > <environment: namespace:IAPWS95> > > Results: > >> initial2 > T p rho > 1 279 0.46 1000.12283 > 2 294 0.46 1000.12283 > > > What the results should be: > >> initial2 > T p rho > 1 279 0.46 1000.12283 > 2 294 0.46 998.19167 > > I think something is evaluating incorrectly, but I don't know enough about NSE vs SE to figure this out. > > Any thoughts to what could be the reason why the second row has the same rho as the first? When I run the function DTp individually, I get the right results. > > > Shawn Way > > > > ______________________________________________ > R-help at r-project.org 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.