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]]
Berend Hasselman
2013-Oct-24 15:51 UTC
[R] R: Optional argument to be used in a subset function
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.
Sorry for the html code: 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 On Thu, Oct 24, 2013 at 8:15 AM, David Mora <dmvxic@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]]