doFxBySort: Call a MakeMyForests Function On Subsetted Data

Description Usage Arguments Value Examples

Description

This is a general function to call a function (fn) on a dataframe (doCols) for each of a set of unique search criteria (searchCols). You must also include the full dataframe (a dataframe with all searchCols and doCols), and, if necessary, any extra parameters needed by the function to run.

Usage

1
2
doFxBySort(fn, searchCols, doCols, fulldf, extraParams = NULL,
  includeSort = FALSE)

Arguments

fn

The name of the function to call. Can be a function outside of MakeMyForests, e.g., "mean".

searchCols

A single string or vector of strings containing the names of the columns to sort by.

doCols

A single string or vector of strings containing the names of the columns to use for function evaluation.

fulldf

The dataframe to use for searching and doing. Must contain the column names contained within both searchCols and doCols.

extraParams

Any extra parameters, if needed, to correctly run the function being called.

includeSort

Set this value to TRUE if you want to see the sort columns alongside the response variables.

Value

This function returns either a vector of responses or a dataframe with sort values.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
myDf <- data.frame(type=c(rep("vegetable", 10), rep("fruit", 10)),
species=c(rep("broccoli",5), rep("beans", 5), rep("apples", 5),
rep("oranges", 5)), weights=as.numeric(runif(20, 0, 2)))
doFxBySort("mean", c("type", "species"), "weights", myDf)

doFxBySort("mean", c("type", "species"),
"weights", myDf, includeSort=TRUE)

myDfNA <- rbind(myDf, c("vegetable", "broccoli", NA))
## the above line converts the weights row to a character type, convert it back
myDfNA$weights <- as.numeric(myDfNA$weights)
## do fx again
doFxBySort("mean", c("type", "species"), "weights",
myDfNA, extraParam=list(na.rm=TRUE))

doFxBySort("mean", c("type", "species"), "weights",
myDfNA, extraParam=list(na.rm=TRUE), includeSort=TRUE)

ecology-rocks/MakeMyForests documentation built on May 15, 2019, 7:57 p.m.