[R] R: Optional argument to be used in a subset function

Berend Hasselman bhh at xs4all.nl
Thu Oct 24 17:51:57 CEST 2013


On 24-10-2013, at 16:15, David Mora <dmvxic at gmail.com> wrote:

> I'm writing a function that needs an optional variable. If the variable is
> given, then a subset must be made using that variable, but if the variable
> is not given the subset must be done using all the values for that variable
> (ignoring that variable).
> 
> Something like this:
> 
> *band <- function(file, fruit = "apple", optional=TRUE) {*
> 
> *data <- read.csv(file)*
> 
> *a <- nrow(subset(data, 'Column 1' == fruit & 'Column 2'=="hot" & 'Column 3'==
> optional))*
> 
> *b <- nrow(subset(data,'Column 1' == fruit & 'Column 2'=="cold" & 'Column 3'==
> optional)) *
> 
> *z <- a+b*
> 
> * print(z)*
> 
> * }*
> 
> What I need is that the function *band("file","orange")*,  subset the data
> frame using all possible values for Column 3 but is not doing it.
> 
> All ideas are welcome.
> 
> Thanks.
> 
> 
> David Mora
> 
> 	[[alternative HTML version deleted]]
> 

Please do not post in HTML.
And make stuff bold: in plain text it is converted to things like this:

* }*

which nonsense for R.

Berend

> ______________________________________________
> R-help at r-project.org mailing list
> 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.



More information about the R-help mailing list