[R] Graphing a subset of data

David Winsemius dwinsemius at comcast.net
Tue Dec 1 04:15:19 CET 2015


Dear Alex (as you are signing yourself on StackOverflow);

It is considered poor manners to cross-post identical questions in multiple venues. 

http://stackoverflow.com/questions/34011669/graphing-function-based-on-subset-of-data

You should choose one or the other of SO and Rhelp. If you do not get a satisfying answer in your first choice, you should wait an appropriate number of hours before posting at the other venue. And when you do end up cross-posting, your should stated where else the question was asked so that potential respondents can check to see if you have already gotten an answer.

You should also read the the posting guide where it is clearly stated the rhelp responders expect that you include a dataset built with R code for purposes of illustration.

> On Nov 30, 2015, at 6:44 PM, Alexandra Hua <alexandra.hua at yale.edu> wrote:
> 
> I am trying to write a function that will graph a variable of a dataset or
> a variable from a subset of the data. My function is as follows:
> 
> graphics<-function(dat, var, graph, varname, val, subset){
>  if(subset==1) {
>    data<-mySubset(dat=dat,varname=varname,val=val)
>  }else if(subset!=1){
>    data<-(dat)
>  }
>  if(graph==1) {
>    with(data, boxplot(var), main = paste("Vertical box plot of",
> myfunc(dat), "variable", myfunc(var), xlab = myfunc(var)))
>  }else if(graph==2){
>    with(data, boxplot(var), horizontal=TRUE, main = paste("Horizontal
> box plot of", myfunc(dat), "variable", myfunc(var), xlab =
> myfunc(var)))
>  }else if(graph==3){
>    hist(var, main="Histogram of", myfunc(var))
>  }}
> 

Generally the use of `with` inside functions is ill-advised. Sometimes it succeeds but nmany times it will fail with puzzling error messages.


> I included "subset" as a parameter for the function, so that subset=1 would
> tell the function to subset, and any other value will use the full dataset.
> However, when I run the function with the following expression (bolts is
> the dataset, SPEED1 is the variable, value=3)
> 
> graphics(bolts, bolts$SPEED1, graph=3, bolts$SPEED1, 3, 1)
> 
> I receive this error message: Error in eval(substitute(expr), data, enclos
> = parent.frame()) : invalid 'envir' argument of type 'logical'
> 
> Does anyone know why this is, or if there is something wrong with my code?
> Thanks!
> 
> -- 
> *Alexandra Hua *
> Yale University | MPH Candidate Class of 2016
> Chronic Disease Epidemiology
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list