loops - Iterate boxplot R model.frame.default -
i have question regarding looping data frame , making boxplot of each numerical column of data frame dependent on data frame. example, mtcars: providing boxplot of each column vs. gear column.
i tried following:
for (i in names(mtcars)){ boxplot(i ~ gear, data = mtcars) }
this, however, results in following error:
"error in model.frame.default(formula = ~ gear, data = mtcars) : variable lengths differ (found 'gear')"
i know there other posts on stackoverflow show in cases na values prohibit kind of formula working, mtcars dataset complete dataset, na values cannot issue.
my question: how can create boxplot each variable vs. 'fixed' variable data frame? example:
boxplot(mpg ~ gear, data = mtcars) boxplot(cyl ~ gear, data = mtcars)
and on.
other posts relating error in context can found here.
unfortunately not able solve problem answers described here, because in cases na values problem, or more technical extracting elements list when using user-defined function.
just add get
before i
in loop. question not clear output should be. if may continue loop
, can this:
for(i in names(mtcars)){ with(mtcars, boxplot(get(i) ~ gear)) }
Comments
Post a Comment