On Sun, Jan 23, 2011 at 6:56 AM, Vitalie S. <spinuvit.l...@gmail.com> wrote: > Gabor Grothendieck <ggrothendi...@gmail.com> writes: > >> On Sat, Jan 22, 2011 at 3:08 PM, Vitalie S. <spinuvit.l...@gmail.com> wrote: >>> >>> Hello everyone! >>> >>> Motivated by the recent post on SO >>> >> http://stackoverflow.com/questions/4730551/making-a-string-concatenation-operator-in-r> >>> I wonder what is the current state of argument on making "+" to >>> concatenate character vectors. The "+" method is still sealed for >>> signature("character", "character") in the current version of R. >>> >>> The 4 years old R-devel thread >>> https://www.stat.math.ethz.ch/pipermail/r-devel/2006-August/038991.html> >>> on the same topic, stopped without reaching any definite conclusion. >>> >>> The only definite argument occurred in the thread against "+" operator >>> was the lack of commutativity (as if one have to prove algebraic >>> theorems in R). >>> >>> Yet another useful suggestion of introducing cat0() and paste0(), for >>> the common use of cat and paste with sep="" was not absorbed by the >>> core R either. >> >> The gsubfn package has always had a paste0 function and I would be >> happy to remove it if the core adds it. >> >> Also the gsubfn supports quasi perl style string interpolation that >> can sometimes be used to avoid the use of paste in the first place. >> Just preface the function in question by fn$ like this: >> >> library(gsubfn) >> fn$cat("pi = $pi\n") > > Thanks for the tip. Not bad indeed. > Almost as readable as > > cat("pi = " + pi + "\n")
To me the + can be substantially less readable. The need to repeatedly quote everything makes it just as bad as paste. Compare the following and try to figure out if there is an error in quoting in the + and paste solutions. Trying to distinguish the single and double quotes is pretty difficult but simple in the fn$ and sprintf solutions. Even if there were no quotes the constant need to interpose quotes makes it hard to read. library(sqldf) # also pulls in gsubfn which has fn$ and paste0 plant <- "Qn1" treatment <- "nonchilled" # using + # sqldf("select * from CO2 where Plant = '" + plant + "' and Treatment = '" + treatment + "' limit 10") # using paste0, also from gsubfn sqldf(paste0("select * from CO2 where Plant = '", plant, "' and Treatment = '", treatment, "' limit 10")) # using paste, almost the same as last one sqldf(paste("select * from CO2 where Plant = '", plant, "' and Treatment = '", treatment, "' limit 10", sep = "")) # With the perl-like interpolation you don't need the repeated quoting in the first place so its much clearer. # using perl-like interpolation from gsubfn fn$sqldf("select * from CO2 where Plant = '$plant' and Treatment = '$treatment' limit 10") # sprintf is nearly as good as the perl-like interpolation except you have to match up % codes and arguments which is a bit of nuisance # and there are more parentheses. On the other hand it does have the advantage that there is the facility for fancier formatting codes # (though this example does not illustrate that aspect): # using sprintf sqldf(sprintf("select * from CO2 where Plant = '%s' and Treatment = '%s' limit 10", plant, treatment)) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com ______________________________________________ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel