r - How can you anticipate the correct way to quote a variable? -
there must pattern/rationale here, can't see it. how quote variables right first time?
require(ggplot2) require(reshape2) require(plyr) # reshape2 # these work. dcast(mpg, manufacturer ~ class, mean, value.var = "cty") dcast(mpg, "manufacturer ~ class", mean, value.var = "cty") # these don't. dcast(mpg, .(manufacturer ~ class), mean, value.var = "cty") dcast(mpg, manufacturer ~ class, mean, value.var = cty) dcast(mpg, manufacturer ~ class, mean, value.var = .(cty)) # plyr # these work. ddply(mpg, .(manufacturer), summarize, mean = mean(cty)) ddply(mpg, "manufacturer", summarize, mean = mean(cty)) ddply(mpg, manufacturer ~ class, summarize, mean = mean(cty)) # these don't. ddply(mpg, manufacturer, summarize, mean = mean(cty)) ddply(mpg, .(manufacturer), summarize, mean = mean(.(cty))) ddply(mpg, .(manufacturer), summarize, mean = mean("cty")) ddply(mpg, .(manufacturer ~ class), summarize, mean = mean(cty)) ddply(mpg, "manufacturer ~ class", summarize, mean = mean(cty)) # ggplot # works qplot(displ, hwy, data = mpg) # these don't qplot(.(displ), .(hwy), data = mpg) qplot("displ", "hwy", data = mpg) p <- qplot(displ, hwy, data = mpg) # these work p + facet_wrap(~ cyl) p + facet_wrap(.(cyl)) p + facet_wrap("cyl") # doesn't p + facet_wrap(cyl)
feel free add missing permutations , combinations.
an argument format entirely depends on function implementation. function page describes way have provide them.
take ddply
function plyr
. second argument called .variables
, described :
.variables: variables split data frame by, ‘as.quoted’ variables, formula or character vector
that's why can input them .(manufacturer)
(as.quoted
variables), "manufacturer"
(character vector), or manufacturer~class
(formula).
you can judge them non-consistent, know how quote arguments have understand way function works , expecting input...
Comments
Post a Comment