Re: [R] why the length and width of a plot region produced by the dev.new() function cannot be correctly set?
> On May 3, 2018, at 6:28 PM, sunyeping via R-helpwrote: > > When I check the size of the plot region usingdev.size("in")a new plot region > is produced and in the Rconsole I get[1] 5.33 5.322917 Your test is all mangleed together. You failed in your duty to read the list info and the Posting guide . NO HTML! > If I mean to produce a plot region with size setting > bydev.new(length=3,width=3)a plot region is produced, but the size is > [2.281250, 5.322917], as detected by the de.size function. If I > type:dev.new(length=10,width=10)I get a plot region of with the size of > [7.614583, 5.322917]. It seems that the width of the new plot region cannot > be set, and tt is always 5.322917. The length of the new plot region can be > set, but it is always smaller that the values I set.What do I miss? What is > the correct way of setting the dimension of the new plot region? I will be > grateful to any help.Best regards, The size of the device is not the size of the plot region. You need to take into account the margins. See ?par > > [[alternative HTML version deleted]] > > __ > R-help@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. David Winsemius Alameda, CA, USA 'Any technology distinguishable from magic is insufficiently advanced.' -Gehm's Corollary to Clarke's Third Law __ R-help@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.
[R] why the length and width of a plot region produced by the dev.new() function cannot be correctly set?
When I check the size of the plot region usingdev.size("in")a new plot region is produced and in the Rconsole I get[1] 5.33 5.322917If I mean to produce a plot region with size setting bydev.new(length=3,width=3)a plot region is produced, but the size is [2.281250, 5.322917], as detected by the de.size function. If I type:dev.new(length=10,width=10)I get a plot region of with the size of [7.614583, 5.322917]. It seems that the width of the new plot region cannot be set, and tt is always 5.322917. The length of the new plot region can be set, but it is always smaller that the values I set.What do I miss? What is the correct way of setting the dimension of the new plot region? I will be grateful to any help.Best regards, [[alternative HTML version deleted]] __ R-help@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.
Re: [R] adding overall constraint in optim()
On Thu, May 3, 2018 at 2:03 PM, Michael Ashtonwrote: > Thanks Bert. But everyone on that forum wants to use finance tools rather > than general optimization stuff! And I am not optimizing a traditional > Markowitz mean-variance problem. Plus, smarter people here. :-) > I'm very confused by these statements. Most of the "finance tools" use general-purpose global and/or stochastic optimization packages (e.g. rugarch uses nloptr and Rsolnp, PortfolioAnalytics uses DEoptim, pso, GenSA). And most (all?) of those optimization packages have ways to specify box, equality, and nonlinear inequality constraints. And I can't recall the last time someone emailed the list about optimizing a traditional Markowitz mean-variance problem... maybe 10 years ago? >> On May 3, 2018, at 3:01 PM, Bert Gunter wrote: >> >> You can't -- at least as I read the docs for ?optim (but I'm pretty >> ignorant about this, so maybe there's a way to tweak it so you can). >> >> See here: https://cran.r-project.org/web/views/Optimization.html >> for other R optimization capabilities. >> >> Also, given your credentials, the r-sig-finance list might be a >> better place for you to post your query. >> >> Cheers, >> Bert >> >> >> Bert Gunter >> >> "The trouble with having an open mind is that people keep coming along >> and sticking things into it." >> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) >> >> >> On Thu, May 3, 2018 at 10:52 AM, Michael Ashton >> wrote: >>> Hi – >>> >>> This is giving me a headache. I’m trying to do a relatively simple >>> optimization – actually trying to approximate the output from the Excel >>> Solver function but at roughly 1000x the speed. >>> >>> The optimization parameters look like this. The only trouble is that I want >>> to add a constraint that sum(wgt.vect)=1, and I can’t figure out how to do >>> that in optim. >>> >>> Mo.vect <- as.vector(tail(head(mo,i),1)) >>> wgt.vect <- as.vector(tail(head(moWeightsMax,i),1)) >>> cov.mat <- cov(tail(head(morets,i+12),12)) >>> opt.fun <- function(wgt.vect) -sum(Mo.vect %*% wgt.vect) / (t(wgt.vect) >>> %*% (cov.mat %*% wgt.vect)) >>> >>> LowerBounds<-c(0.2,0.05,0.1,0,0,0) >>> UpperBounds<-c(0.6,0.3,0.6,0.15,0.1,0.2) >>> >>> OptimSolution<-optim(wgt.vect, fn=opt.fun, >>> method="L-BFGS-B",lower=LowerBounds,upper=UpperBounds) >>> >>> >>> Any thoughts are appreciated! >>> >>> Mike >>> >>> Michael Ashton, CFA >>> Managing Principal >>> >>> Enduring Investments LLC >>> W: 973.457.4602 >>> C: 551.655.8006 >>> >>> >>>[[alternative HTML version deleted]] >>> >>> __ >>> R-help@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. > __ > R-help@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. -- Joshua Ulrich | about.me/joshuaulrich FOSS Trading | www.fosstrading.com R/Finance 2018 | www.rinfinance.com __ R-help@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.
Re: [R] Converting a list to a data frame
If you require that the 'type' column be a factor with a level for each element of the input list, then you need to do that after calling dplyr::bind_rows(), just as with the base-R solutions. > l <- list( A = data.frame(X=1:2, Y=11:12), B = data.frame(X=integer(), Y=integer()), C = data.frame(X=3L, Y=13L) ) > str(d <- dplyr::bind_rows(l, .id = "Which") ) 'data.frame': 3 obs. of 3 variables: $ Which: chr "A" "A" "C" $ X: int 1 2 3 $ Y: int 11 12 13 > d$Which <- factor(d$Which, levels=names(l)) > str(d) 'data.frame': 3 obs. of 3 variables: $ Which: Factor w/ 3 levels "A","B","C": 1 1 3 $ X: int 1 2 3 $ Y: int 11 12 13 Sometimes you need the names of the of the 0-row data.frames in the output to make plots, etc., comparable across various samples of the data. Bill Dunlap TIBCO Software wdunlap tibco.com On Thu, May 3, 2018 at 10:28 AM, Hadley Wickhamwrote: > On Wed, May 2, 2018 at 11:53 AM, Jeff Newmiller > wrote: > > Another approach: > > > > > > library(tidyr) > > L <- list( A = data.frame( x=1:2, y=3:4 ) > > , B = data.frame( x=5:6, y=7:8 ) > > ) > > D <- data.frame( Type = names( L ) > >, stringsAsFactors = FALSE > >) > > D$data <- L > > unnest(D, data) > > #> Type x y > > #> 1A 1 3 > > #> 2A 2 4 > > #> 3B 5 7 > > #> 4B 6 8 > > > > I think a slightly more idiomatic tidyverse solution is dplyr::bind_rows() > > l <- list( > A = data.frame(x = 1:2, y = 3:4), > B = data.frame(x = 5:6, y = 7:8) > ) > > dplyr::bind_rows(l, .id = "type") > #> type x y > #> 1A 1 3 > #> 2A 2 4 > #> 3B 5 7 > #> 4B 6 8 > > This also has the advantage of returning a data frame when the inputs > are data frames. > > Hadley > > -- > http://hadley.nz > > __ > R-help@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. > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] adding overall constraint in optim()
This looks like what I call a sumscale problem i.e., some sort of simple function of the parameters sums to a constant. I've done some work on these, but don't have it with me just now. There are several approaches, but they can be quite tricky. Will send some info in about a week or so if you are still stuck and contact me offline. JN (you'll see my name on 3/5 of optim routines via ?optim) On 2018-05-03 01:52 PM, Michael Ashton wrote: > Hi – > > This is giving me a headache. I’m trying to do a relatively simple > optimization – actually trying to approximate the output from the Excel > Solver function but at roughly 1000x the speed. > > The optimization parameters look like this. The only trouble is that I want > to add a constraint that sum(wgt.vect)=1, and I can’t figure out how to do > that in optim. > > Mo.vect <- as.vector(tail(head(mo,i),1)) > wgt.vect <- as.vector(tail(head(moWeightsMax,i),1)) > cov.mat <- cov(tail(head(morets,i+12),12)) > opt.fun <- function(wgt.vect) -sum(Mo.vect %*% wgt.vect) / (t(wgt.vect) > %*% (cov.mat %*% wgt.vect)) > > LowerBounds<-c(0.2,0.05,0.1,0,0,0) > UpperBounds<-c(0.6,0.3,0.6,0.15,0.1,0.2) > > OptimSolution<-optim(wgt.vect, fn=opt.fun, > method="L-BFGS-B",lower=LowerBounds,upper=UpperBounds) > > > Any thoughts are appreciated! > > Mike > > Michael Ashton, CFA > Managing Principal > > Enduring Investments LLC > W: 973.457.4602 > C: 551.655.8006 > > > [[alternative HTML version deleted]] > > __ > R-help@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. > __ R-help@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.
Re: [R] adding overall constraint in optim()
> On May 3, 2018, at 10:52 AM, Michael Ashton >wrote: > > Hi – > > This is giving me a headache. I’m trying to do a relatively simple > optimization – actually trying to approximate the output from the Excel > Solver function but at roughly 1000x the speed. > > The optimization parameters look like this. The only trouble is that I want > to add a constraint that sum(wgt.vect)=1, and I can’t figure out how to do > that in optim. > > Mo.vect <- as.vector(tail(head(mo,i),1)) > wgt.vect <- as.vector(tail(head(moWeightsMax,i),1)) > cov.mat <- cov(tail(head(morets,i+12),12)) > opt.fun <- function(wgt.vect) -sum(Mo.vect %*% wgt.vect) / (t(wgt.vect) > %*% (cov.mat %*% wgt.vect)) > > LowerBounds<-c(0.2,0.05,0.1,0,0,0) > UpperBounds<-c(0.6,0.3,0.6,0.15,0.1,0.2) > > OptimSolution<-optim(wgt.vect, fn=opt.fun, > method="L-BFGS-B",lower=LowerBounds,upper=UpperBounds) > > > Any thoughts are appreciated! Have you reviewed the documentation for constrOptim {in pkg-stats}? > > > [[alternative HTML version deleted]] I was surprised that emoji made it through. Nonetheless it is usually much safer to posting in plain-text. Didn't seem to be a problem in this case but code and data are often mangled. David Winsemius Alameda, CA, USA 'Any technology distinguishable from magic is insufficiently advanced.' -Gehm's Corollary to Clarke's Third Law __ R-help@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.
Re: [R] adding overall constraint in optim()
Thanks Bert. But everyone on that forum wants to use finance tools rather than general optimization stuff! And I am not optimizing a traditional Markowitz mean-variance problem. Plus, smarter people here. :-) > On May 3, 2018, at 3:01 PM, Bert Gunterwrote: > > You can't -- at least as I read the docs for ?optim (but I'm pretty > ignorant about this, so maybe there's a way to tweak it so you can). > > See here: https://cran.r-project.org/web/views/Optimization.html > for other R optimization capabilities. > > Also, given your credentials, the r-sig-finance list might be a > better place for you to post your query. > > Cheers, > Bert > > > Bert Gunter > > "The trouble with having an open mind is that people keep coming along > and sticking things into it." > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > > On Thu, May 3, 2018 at 10:52 AM, Michael Ashton > wrote: >> Hi – >> >> This is giving me a headache. I’m trying to do a relatively simple >> optimization – actually trying to approximate the output from the Excel >> Solver function but at roughly 1000x the speed. >> >> The optimization parameters look like this. The only trouble is that I want >> to add a constraint that sum(wgt.vect)=1, and I can’t figure out how to do >> that in optim. >> >> Mo.vect <- as.vector(tail(head(mo,i),1)) >> wgt.vect <- as.vector(tail(head(moWeightsMax,i),1)) >> cov.mat <- cov(tail(head(morets,i+12),12)) >> opt.fun <- function(wgt.vect) -sum(Mo.vect %*% wgt.vect) / (t(wgt.vect) >> %*% (cov.mat %*% wgt.vect)) >> >> LowerBounds<-c(0.2,0.05,0.1,0,0,0) >> UpperBounds<-c(0.6,0.3,0.6,0.15,0.1,0.2) >> >> OptimSolution<-optim(wgt.vect, fn=opt.fun, >> method="L-BFGS-B",lower=LowerBounds,upper=UpperBounds) >> >> >> Any thoughts are appreciated! >> >> Mike >> >> Michael Ashton, CFA >> Managing Principal >> >> Enduring Investments LLC >> W: 973.457.4602 >> C: 551.655.8006 >> >> >>[[alternative HTML version deleted]] >> >> __ >> R-help@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. __ R-help@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.
Re: [R] adding overall constraint in optim()
You can't -- at least as I read the docs for ?optim (but I'm pretty ignorant about this, so maybe there's a way to tweak it so you can). See here: https://cran.r-project.org/web/views/Optimization.html for other R optimization capabilities. Also, given your credentials, the r-sig-finance list might be a better place for you to post your query. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Thu, May 3, 2018 at 10:52 AM, Michael Ashtonwrote: > Hi – > > This is giving me a headache. I’m trying to do a relatively simple > optimization – actually trying to approximate the output from the Excel > Solver function but at roughly 1000x the speed. > > The optimization parameters look like this. The only trouble is that I want > to add a constraint that sum(wgt.vect)=1, and I can’t figure out how to do > that in optim. > > Mo.vect <- as.vector(tail(head(mo,i),1)) > wgt.vect <- as.vector(tail(head(moWeightsMax,i),1)) > cov.mat <- cov(tail(head(morets,i+12),12)) > opt.fun <- function(wgt.vect) -sum(Mo.vect %*% wgt.vect) / (t(wgt.vect) > %*% (cov.mat %*% wgt.vect)) > > LowerBounds<-c(0.2,0.05,0.1,0,0,0) > UpperBounds<-c(0.6,0.3,0.6,0.15,0.1,0.2) > > OptimSolution<-optim(wgt.vect, fn=opt.fun, > method="L-BFGS-B",lower=LowerBounds,upper=UpperBounds) > > > Any thoughts are appreciated! > > Mike > > Michael Ashton, CFA > Managing Principal > > Enduring Investments LLC > W: 973.457.4602 > C: 551.655.8006 > > > [[alternative HTML version deleted]] > > __ > R-help@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. __ R-help@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.
Re: [R] MCMCglmm - metric of the estimates
The r-sig-mixed-models list would be a better place for this post if you do not get an answer here. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Thu, May 3, 2018 at 10:56 AM, Petr Palíšekwrote: > Hi, > > my question is probably amateurish but I can't seem to find the answer > anywhere. > > In what metric are the MCMCglmm package's posterior means for family = > "categorical"? > > I suppose that they can't be odds ratios and probabilites as my numbers are > outside their bounds. So I'm thinking – are they just basic regression > coefficients conceptually equal to those obtained by lme4::glmer? > > Thank you! > > [[alternative HTML version deleted]] > > __ > R-help@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. __ R-help@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.
[R] MCMCglmm - metric of the estimates
Hi, my question is probably amateurish but I can't seem to find the answer anywhere. In what metric are the MCMCglmm package's posterior means for family = "categorical"? I suppose that they can't be odds ratios and probabilites as my numbers are outside their bounds. So I'm thinking – are they just basic regression coefficients conceptually equal to those obtained by lme4::glmer? Thank you! [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Converting a list to a data frame
On 05/03/2018 01:28 PM, Hadley Wickham wrote: On Wed, May 2, 2018 at 11:53 AM, Jeff Newmillerwrote: Another approach: library(tidyr) L <- list( A = data.frame( x=1:2, y=3:4 ) , B = data.frame( x=5:6, y=7:8 ) ) D <- data.frame( Type = names( L ) , stringsAsFactors = FALSE ) D$data <- L unnest(D, data) #> Type x y #> 1A 1 3 #> 2A 2 4 #> 3B 5 7 #> 4B 6 8 I think a slightly more idiomatic tidyverse solution is dplyr::bind_rows() l <- list( A = data.frame(x = 1:2, y = 3:4), B = data.frame(x = 5:6, y = 7:8) ) dplyr::bind_rows(l, .id = "type") #> type x y #> 1A 1 3 #> 2A 2 4 #> 3B 5 7 #> 4B 6 8 This also has the advantage of returning a data frame when the inputs are data frames. Hadley I _clearly_ need to learn the dplyr package. -- Kevin E. Thorpe Head of Biostatistics, Applied Health Research Centre (AHRC) Li Ka Shing Knowledge Institute of St. Michael's Hospital Assistant Professor, Dalla Lana School of Public Health University of Toronto email: kevin.tho...@utoronto.ca Tel: 416.864.5776 Fax: 416.864.3016 __ R-help@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.
[R] adding overall constraint in optim()
Hi – This is giving me a headache. I’m trying to do a relatively simple optimization – actually trying to approximate the output from the Excel Solver function but at roughly 1000x the speed. The optimization parameters look like this. The only trouble is that I want to add a constraint that sum(wgt.vect)=1, and I can’t figure out how to do that in optim. Mo.vect <- as.vector(tail(head(mo,i),1)) wgt.vect <- as.vector(tail(head(moWeightsMax,i),1)) cov.mat <- cov(tail(head(morets,i+12),12)) opt.fun <- function(wgt.vect) -sum(Mo.vect %*% wgt.vect) / (t(wgt.vect) %*% (cov.mat %*% wgt.vect)) LowerBounds<-c(0.2,0.05,0.1,0,0,0) UpperBounds<-c(0.6,0.3,0.6,0.15,0.1,0.2) OptimSolution<-optim(wgt.vect, fn=opt.fun, method="L-BFGS-B",lower=LowerBounds,upper=UpperBounds) Any thoughts are appreciated! Mike Michael Ashton, CFA Managing Principal Enduring Investments LLC W: 973.457.4602 C: 551.655.8006 [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Converting a list to a data frame
On Wed, May 2, 2018 at 11:53 AM, Jeff Newmillerwrote: > Another approach: > > > library(tidyr) > L <- list( A = data.frame( x=1:2, y=3:4 ) > , B = data.frame( x=5:6, y=7:8 ) > ) > D <- data.frame( Type = names( L ) >, stringsAsFactors = FALSE >) > D$data <- L > unnest(D, data) > #> Type x y > #> 1A 1 3 > #> 2A 2 4 > #> 3B 5 7 > #> 4B 6 8 > I think a slightly more idiomatic tidyverse solution is dplyr::bind_rows() l <- list( A = data.frame(x = 1:2, y = 3:4), B = data.frame(x = 5:6, y = 7:8) ) dplyr::bind_rows(l, .id = "type") #> type x y #> 1A 1 3 #> 2A 2 4 #> 3B 5 7 #> 4B 6 8 This also has the advantage of returning a data frame when the inputs are data frames. Hadley -- http://hadley.nz __ R-help@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.
Re: [R] Converting a list to a data frame
> On May 2, 2018, at 2:43 PM, David L Carlsonwrote: > > Typo: dat[[z]] should be x[[z]]: > > x2 <- do.call(rbind, lapply(names(x), function(z) > data.frame(type=z, x[[z]]))) > x2 > type x y > 1A 1 3 > 2A 2 4 > 3B 5 7 > 4B 6 8 Sometimes one want to "bind" list that have some columns in common and others that you want to include from one but leave as NA for the rows from the other. The rbind.list function from package-plyr is from the pre-tidy/tibble era of Hadley's efforts and provides that facility out of the box. It also seems to "work" with the do.call(rbind strategy for named lists above. x <- list(A=mtcars[c("mpg", "wt")] , B=mtcars[c("wt", "cyl")]) #mtcars x2 <- do.call(rbind.fill, lapply(names(x), function(z) data.frame(type=z, x[[z]]))) str(x2) #- 'data.frame': 64 obs. of 4 variables: type: Factor w/ 2 levels "A","B": 1 1 1 1 1 1 1 1 1 1 ... mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ... wt : num 2.62 2.88 2.32 3.21 3.44 ... cyl : num NA NA NA NA NA NA NA NA NA NA ... -- David, the other. > > David C > > -Original Message- > From: R-help On Behalf Of David L Carlson > Sent: Wednesday, May 2, 2018 3:51 PM > To: William Dunlap ; Kevin E. Thorpe > > Cc: r-help mailing list > Subject: Re: [R] Converting a list to a data frame > > Or add the type column first and then rbind: > > x <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8)) > x2 <- do.call(rbind, lapply(names(x), function(z) > data.frame(type=z, dat[[z]]))) > > > David L Carlson > Department of Anthropology > Texas A University > College Station, TX 77843-4352 > > -Original Message- > From: R-help On Behalf Of William Dunlap via > R-help > Sent: Wednesday, May 2, 2018 12:28 PM > To: Kevin E. Thorpe > Cc: R Help Mailing List > Subject: Re: [R] Converting a list to a data frame > >> x1 <- do.call(rbind, c(x, list(make.row.names=FALSE))) >> x2 <- cbind(type=rep(names(x), vapply(x, nrow, 0)), x1) >> str(x2) > 'data.frame': 4 obs. of 3 variables: > $ type: Factor w/ 2 levels "A","B": 1 1 2 2 > $ x : int 1 2 5 6 > $ y : int 3 4 7 8 > > > Bill Dunlap > TIBCO Software > wdunlap tibco.com > > On Wed, May 2, 2018 at 10:11 AM, Kevin E. Thorpe > wrote: > >> I suspect this is pretty easy, but I'm having trouble figuring it out. >> Basically, I have a list of data frames such as the following example: >> >> list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8)) >> >> I would like to turn this into data frame where the list elements are >> essentially rbind'ed together and the element name becomes a new variable. >> For example, I would like to turn the list above into a data frame >> that looks like this: >> >> data.frame(type=c("A","A","B","B"),x=c(1:2,5:6),y=c(3:4,7:8)) >> >> Appreciate any pointers. >> >> Kevin >> >> -- >> Kevin E. Thorpe >> Head of Biostatistics, Applied Health Research Centre (AHRC) Li Ka >> Shing Knowledge Institute of St. Michael's Hospital Assistant >> Professor, Dalla Lana School of Public Health University of Toronto >> email: kevin.tho...@utoronto.ca Tel: 416.864.5776 Fax: 416.864.3016 >> >> __ >> R-help@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/posti >> ng-guide.html and provide commented, minimal, self-contained, >> reproducible code. >> > > [[alternative HTML version deleted]] > > __ > R-help@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. > > __ > R-help@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. > > __ > R-help@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. David Winsemius Alameda, CA, USA 'Any technology distinguishable from magic is insufficiently advanced.' -Gehm's Corollary to Clarke's Third Law __ R-help@r-project.org mailing list -- To
Re: [R] Calling the curve function with a character object converted into an expression
Thanks, I always get confused by expression evaluation, when and how to use call, do.call, eval, parse/deparse, and all that good stuff. I always have to read documentation 10 times and still does not want to stick in my brain. - Original Message - From: "Bert Gunter"To: "Sebastien Bihorel" Cc: "R-help" Sent: Thursday, May 3, 2018 2:28:59 AM Subject: Re: [R] Calling the curve function with a character object converted into an expression Typo: should be NULL not NUL of course An alternative approach closer to your original attempt is to use do.call() to explicitly evaluate the expr argument: w <- "1 + x^2" do.call(curve, list(expr = parse(text = w), ylab ="y")) Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Wed, May 2, 2018 at 10:35 PM, Bert Gunter wrote: > Sebastian: > > This is somewhat arcane, perhaps even a bug (correction on this > welcomed). The problem is that the "expr" argument to curve() must be > an actual expression, not a call to parse that evaluates to an > expression. If you look at the code of curve() you'll see why > (substitute() does not evaluate expr in the code). Another simple > workaround other than sticking in the eval() call is this: > > myf <- function(x)NUL > body(myf)<- parse(text = "{1+x^2}") ## note the additional "{ }" > for safety, though not necessary here > ## this idiom will continue to work for any text. > > curve(myf, from = 0, to = 10) ## myf is now the name of a function > that executes the parsed text. > > *** I would appreciate any wiser R programmers correcting any > misunderstanding or error in my explanation *** > > Cheers, > Bert > > Bert Gunter > > "The trouble with having an open mind is that people keep coming along > and sticking things into it." > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > > On Wed, May 2, 2018 at 8:11 PM, Sebastien Bihorel > wrote: >> >> Hi, >> >> Down a cascade of function calls, I want to use the curve function with an >> expression that is a variable. For various reason, this variable must be a >> character object and cannot be an expression as required by the curve >> function. How do I convert my variable into a expression that is accepted by >> curve? >> >> Thanks in advance for your help. >> >> ## The following attempts do not work >> >> myf <- '1+x^2' >> curve(myf, from = 0, to = 10) # obviously ! >> curve(parse(text=myf), from = 0, to = 10) # not sure why this does not work >> >> ## This works but does not feel elegant: >> eval(parse(text=sprintf('curve(%s, from = 0, to = 10)', myf))) >> >> __ >> R-help@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. __ R-help@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.
Re: [R] The stages of standard function evaluation
First of all, your message is a little hard to read because you posted in HTML. This list removes the HTML, and often mangles messages, so you should always post in plain text. But in this case your message was still pretty readable. On 02/05/2018 11:04 PM, Andrew Hoerner wrote: Dear R Help folks -- I have been trying to put together a list of the steps or stages of R function evaluation, with particular focus on those that have "standard" or "nonstandard" forms. This is both for my own edification and also because I am thinking of joining the world of R bloggers and have been trying to put together some draft posting that might be useful. I seem to have an affirmative genius for finding incorrect interpretations of R's evaluation rules; I'm trying to make that an asset. I am hoping that you can tell me: 1. Is this list complete, or are there additional stages I am missing? 2. Have I inserted one or more imaginary stages? 3. Are the terms I use below to name each stage appropriate, or are there other terms more widely used or recognizable? 4. Is the order correct? I begin each name with “Standard,” to express my belief that each of these things has a usual or default form, but also that (unless I am mistaken) almost none of them exist only in a single form true of all R functions. (I have marked with an asterisk a few evaluation steps that I think may always be followed). It is my ultimate goal (which I do not feel at all close to accomplishing) to determine a way to mechanically test for “standardness” along each of these dimensions, so that each function could be assigned a logical vector showing the ways that it is and is not standard. One thing I think is conceptually or procedurally difficult about this project is that I think “standardness” should be determined by what a function does, rather than by how it does it, so that a primitive function that takes unevaluated arguments positionally could still have standard matching, scoping, etc., by internal emulation. A related goal is to identify which evaluation steps most often use an alternative form, and perhaps determine if there is more than one such alternative. Finally, an easier short-term goal is simply to find instances of one or more function with standard and non-standard evaluation for each evaluation step. For the most part below I am treating the evaluation of closures as the standard from which “nonstandard” is defined. However, I do not assume that other kinds of functions are automatically nonstandard on any particular dimension below. Most of this comes from the R Language Definition, but there are numerous places where I am by no means certain that my interpretation is correct. I have highlighted some of these below with a “??”. I look forward to learning from you. Warmest regards, J. Andrew Hoerner ** Standard function recognition:* recognizing some or all of a string code as a function. (Part of code line parsing) *Standard environment construction:* construction of the execution environment, and of pointers to the calling and enclosing environments. *Standard function identification:* Get the name of the function, if any This may be mangling, but it's really hard to tell whether the 3 paragraphs above are supposed to be steps, headings, or what. Assuming they are steps, the first one is wrong. The parser looks at a string and breaks it down into tokens and subexpressions, making what you later call an AST. The first step in function evaluation is recognizing that something is a function call, not recognizing it as a function. For example, "mean" is the name of a function and also an expression evaluating to a function, "mean(1:10)" is a function call. Once you have a function call, the next step looks at the expression used to specify the function. In "mean(1:10)", that expression is "mean", but it could be an arbitrary R expression. If it is a name like "mean" (or a string), then R looks for an object of mode "function" of that name in the current evaluation frame, or its parent frames. These are not "constructed"; the current evaluation frame is always known, and contains a pointer to its parent. If the function is specified by a more complex expression (e.g. in "fn[[1]](1:10)", the expression is "fn[[1]]") then that expression is evaluated. It needs to return a function object or an error will be generated. So these work: mean(1:10) list(mean)[[1]](1:10) "mean"(1:10) and these don't: list("mean")(1:10) c("mean")(1:10) So now we have the function. Its name is irrelevant. ** Standard f**unction scoping*: Search the current environment and then up the chain of enclosing environments until you find the first binding environment, an environment where the name of the function is bound, i.e. linked to a pointer to the function definition. The binding environment is usually (but not always) the same as the defining environment (i.e. the enclosing
Re: [R] Converting a list to a data frame
On 05/03/2018 03:59 AM, Martin Maechler wrote: David L Carlsonon Wed, 2 May 2018 21:43:52 + writes: Typo: dat[[z]] should be x[[z]]: x2 <- do.call(rbind, lapply(names(x), function(z) data.frame(type=z, x[[z]]))) x2 type x y 1A 1 3 2A 2 4 3B 5 7 4B 6 8 David C Before this thread gets carried away to data.table and tibbleverse (and as nobody else has done so) : Let me nominate this beautiful solution by Bill Dunlap and David Carlson to win the prize with a 10 out 10 grade: Beautiful use of do.call() and lapply(), two of the most versatile and important functions from the base R toolbox. Congratulations! Martin Maechler R Core Team First of all, thank you to all who responded. I have learned (and will learn) about several new things. The data.table/tibbleverse is one of those places I have yet to tread but it never seems to be a "good" time to learn as generally when these tools would be helpful is when I am trying to get something done but don't have the time to recast the problem in a different paradigm. Second, I echo Martin's comment. The combined solution of Bill Dunlap and David Carlson is beautifully elegant. Thanks again, Kevin -Original Message- From: R-help On Behalf Of David L Carlson Sent: Wednesday, May 2, 2018 3:51 PM To: William Dunlap ; Kevin E. Thorpe Cc: r-help mailing list Subject: Re: [R] Converting a list to a data frame Or add the type column first and then rbind: x <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8)) x2 <- do.call(rbind, lapply(names(x), function(z) data.frame(type=z, dat[[z]]))) David L Carlson Department of Anthropology Texas A University College Station, TX 77843-4352 -Original Message- From: R-help On Behalf Of William Dunlap via R-help Sent: Wednesday, May 2, 2018 12:28 PM To: Kevin E. Thorpe Cc: R Help Mailing List Subject: Re: [R] Converting a list to a data frame x1 <- do.call(rbind, c(x, list(make.row.names=FALSE))) x2 <- cbind(type=rep(names(x), vapply(x, nrow, 0)), x1) str(x2) 'data.frame': 4 obs. of 3 variables: $ type: Factor w/ 2 levels "A","B": 1 1 2 2 $ x : int 1 2 5 6 $ y : int 3 4 7 8 Bill Dunlap TIBCO Software wdunlap tibco.com On Wed, May 2, 2018 at 10:11 AM, Kevin E. Thorpe wrote: I suspect this is pretty easy, but I'm having trouble figuring it out. Basically, I have a list of data frames such as the following example: list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8)) I would like to turn this into data frame where the list elements are essentially rbind'ed together and the element name becomes a new variable. For example, I would like to turn the list above into a data frame that looks like this: data.frame(type=c("A","A","B","B"),x=c(1:2,5:6),y=c(3:4,7:8)) Appreciate any pointers. Kevin -- Kevin E. Thorpe Head of Biostatistics, Applied Health Research Centre (AHRC) Li Ka Shing Knowledge Institute of St. Michael's Hospital Assistant Professor, Dalla Lana School of Public Health University of Toronto email: kevin.tho...@utoronto.ca Tel: 416.864.5776 Fax: 416.864.3016 __ R-help@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.
[R] GA/SWARM Hyperparameter (HP) Optimisation for Classification based Machine Learning
Hi, I believe that Caret uses a grid-serach approach. I was wondering if: 1 There are more efficient implementations for HP tuning for classification algos (eg XGboost, CatBoost, SVM, RF etc), using say GM/SWARM approaches, akin to Google's approach AutoML for Image related Net problems? 2 This one is most probably wishful thinking, but is anyone looking at GM/SWARM at HP tuning across models (ensemble models). eg the best set of HP for combined XGBoost + SVM, which accounts for the correlation/interaction of the prediction assumptions. BestAlex [[alternative HTML version deleted]] __ R-help@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.
Re: [ESS] ess-insert-function-outline
G'day Alex, On Wed, 02-May-2018 at 09:19AM -0500, Alex Branham wrote: [...] |> |> It looks like ess-insert-function-outline was retired to the old/ |> directory, so it isn't defined in newer ESS versions. What could be the thinking behind that? The outline file is still there in the etc/ directory, so I never would have imagined dumping ess-insert-function-outline into the old/ directory. |> I'm guessing you updated ESS while tinkering with .emacs/.Rprofile? Good guess. |> |> I never used it myself, but it looks like it's supposed to insert a |> function skeleton? If that's the case, there are a few replacements you |> can use, including the builtin skeleton.el or yasnippet, available on |> ELPA. How do I use that? There's /usr/share/emacs/24.3/lisp/skeleton.elc that I could copy to the lisp/ directory or make a link from there to it. Would that make sense? If not, I'll just stick with an older version that works fine. There's nothing new that makes it worth forgoing the function skeleton. Thanks for your help. -- ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~. ___Patrick Connolly {~._.~} Great minds discuss ideas _( Y )_ Average minds discuss events (:_~*~_:) Small minds discuss people (_)-(_) . Eleanor Roosevelt ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~. __ ESS-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/ess-help
Re: [R] Package for Molecular Properties
library(sos) (mp <- findFn('{molecular properties}')) ** found 7 matches in 4 packages and opened two web pages in my default browser with (a) the 7 matches and (b) the 4 packages. The first function was something for amino acids, like you suggested. Two others returned compound and substance information from PubChem. Does this help? Spencer On 2018-05-02 19:17, bbb_...@verizon.net wrote: All Is there a package or library that will, given a nucleotide sequence 1. calculate the extinction coefficient at 260 nm for (Beer-Lambert's law) 2. calculate molecular weight 3. return it's complementary sequence I was able to find several packages that can do similar calculations for an amino acid sequence for proteins but none for nucleic acids. Any pointers, etc. would be great. thank you in advance Indr __ R-help@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. __ R-help@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.
Re: [R] Converting a list to a data frame
> David L Carlson> on Wed, 2 May 2018 21:43:52 + writes: > Typo: dat[[z]] should be x[[z]]: > > x2 <- do.call(rbind, lapply(names(x), function(z) > data.frame(type=z, x[[z]]))) > x2 > type x y > 1A 1 3 > 2A 2 4 > 3B 5 7 > 4B 6 8 > > David C Before this thread gets carried away to data.table and tibbleverse (and as nobody else has done so) : Let me nominate this beautiful solution by Bill Dunlap and David Carlson to win the prize with a 10 out 10 grade: Beautiful use of do.call() and lapply(), two of the most versatile and important functions from the base R toolbox. Congratulations! Martin Maechler R Core Team > -Original Message- > From: R-help On Behalf Of David L Carlson > Sent: Wednesday, May 2, 2018 3:51 PM > To: William Dunlap ; Kevin E. Thorpe > > Cc: r-help mailing list > Subject: Re: [R] Converting a list to a data frame > > Or add the type column first and then rbind: > > x <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8)) > x2 <- do.call(rbind, lapply(names(x), function(z) > data.frame(type=z, dat[[z]]))) > > > David L Carlson > Department of Anthropology > Texas A University > College Station, TX 77843-4352 > > -Original Message- > From: R-help On Behalf Of William Dunlap via > R-help > Sent: Wednesday, May 2, 2018 12:28 PM > To: Kevin E. Thorpe > Cc: R Help Mailing List > Subject: Re: [R] Converting a list to a data frame > > > x1 <- do.call(rbind, c(x, list(make.row.names=FALSE))) > > x2 <- cbind(type=rep(names(x), vapply(x, nrow, 0)), x1) > > str(x2) > 'data.frame': 4 obs. of 3 variables: > $ type: Factor w/ 2 levels "A","B": 1 1 2 2 > $ x : int 1 2 5 6 > $ y : int 3 4 7 8 > > > Bill Dunlap > TIBCO Software > wdunlap tibco.com > > On Wed, May 2, 2018 at 10:11 AM, Kevin E. Thorpe > wrote: > > > I suspect this is pretty easy, but I'm having trouble figuring it out. > > Basically, I have a list of data frames such as the following example: > > > > list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8)) > > > > I would like to turn this into data frame where the list elements are > > essentially rbind'ed together and the element name becomes a new variable. > > For example, I would like to turn the list above into a data frame > > that looks like this: > > > > data.frame(type=c("A","A","B","B"),x=c(1:2,5:6),y=c(3:4,7:8)) > > > > Appreciate any pointers. > > > > Kevin > > > > -- > > Kevin E. Thorpe > > Head of Biostatistics, Applied Health Research Centre (AHRC) Li Ka > > Shing Knowledge Institute of St. Michael's Hospital Assistant > > Professor, Dalla Lana School of Public Health University of Toronto > > email: kevin.tho...@utoronto.ca Tel: 416.864.5776 Fax: 416.864.3016 > > > __ > R-help@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. __ R-help@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.
[R] Package for Molecular Properties
All Is there a package or library that will, given a nucleotide sequence 1. calculate the extinction coefficient at 260 nm for (Beer-Lambert's law) 2. calculate molecular weight 3. return it's complementary sequence I was able to find several packages that can do similar calculations for an amino acid sequence for proteins but none for nucleic acids. Any pointers, etc. would be great. thank you in advance Indr __ R-help@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.
Re: [R] Converting a list to a data frame
On 05/03/2018 07:58 AM, Jeff Newmiller wrote: This is very nice to learn about, Denis, but it seems only fair to point out that the result of rbindlist is not a data frame. You can convert it to a data frame easily, but the copy and indexing semantics of data tables are quite different than data tables, which could be a real headache for someone not prepared for those differences. (To learn more, read the data tables vignette.) Tibbles (as produced by unnest in my previous response) are not data tables either, but they behave much more like data frames than data tables do. On May 2, 2018 1:30:37 PM MDT, "Tóth Dénes"wrote: On 05/02/2018 07:11 PM, Kevin E. Thorpe wrote: I suspect this is pretty easy, but I'm having trouble figuring it out. Basically, I have a list of data frames such as the following example: list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8)) I would like to turn this into data frame where the list elements are essentially rbind'ed together and the element name becomes a new variable. For example, I would like to turn the list above into a data frame that looks like this: data.frame(type=c("A","A","B","B"),x=c(1:2,5:6),y=c(3:4,7:8)) Appreciate any pointers. Hi Kevin, data.table::rbindlist does exactly what you want in a very efficient way: library(data.table) dat <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8)) rbindlist(dat, idcol = "type") In response to Jeff's note, this a solution which results in a data.frame instead of a data.table: library(data.table) dat <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8)) dat <- rbindlist(dat, idcol = "type") # traditional way dat_df <- as.data.frame(dat) # no copy (assignment not needed, memory-efficient) setDF(dat) Further amendments would be needed to transform the 'type' variable to a factor, if it is required. Regards, Denes Kevin __ R-help@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. __ R-help@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.
Re: [R] Calling the curve function with a character object converted into an expression
Typo: should be NULL not NUL of course An alternative approach closer to your original attempt is to use do.call() to explicitly evaluate the expr argument: w <- "1 + x^2" do.call(curve, list(expr = parse(text = w), ylab ="y")) Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Wed, May 2, 2018 at 10:35 PM, Bert Gunterwrote: > Sebastian: > > This is somewhat arcane, perhaps even a bug (correction on this > welcomed). The problem is that the "expr" argument to curve() must be > an actual expression, not a call to parse that evaluates to an > expression. If you look at the code of curve() you'll see why > (substitute() does not evaluate expr in the code). Another simple > workaround other than sticking in the eval() call is this: > > myf <- function(x)NUL > body(myf)<- parse(text = "{1+x^2}") ## note the additional "{ }" > for safety, though not necessary here > ## this idiom will continue to work for any text. > > curve(myf, from = 0, to = 10) ## myf is now the name of a function > that executes the parsed text. > > *** I would appreciate any wiser R programmers correcting any > misunderstanding or error in my explanation *** > > Cheers, > Bert > > Bert Gunter > > "The trouble with having an open mind is that people keep coming along > and sticking things into it." > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > > On Wed, May 2, 2018 at 8:11 PM, Sebastien Bihorel > wrote: >> >> Hi, >> >> Down a cascade of function calls, I want to use the curve function with an >> expression that is a variable. For various reason, this variable must be a >> character object and cannot be an expression as required by the curve >> function. How do I convert my variable into a expression that is accepted by >> curve? >> >> Thanks in advance for your help. >> >> ## The following attempts do not work >> >> myf <- '1+x^2' >> curve(myf, from = 0, to = 10) # obviously ! >> curve(parse(text=myf), from = 0, to = 10) # not sure why this does not work >> >> ## This works but does not feel elegant: >> eval(parse(text=sprintf('curve(%s, from = 0, to = 10)', myf))) >> >> __ >> R-help@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. __ R-help@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.