combineDf | R Documentation |
This function combines multiple "source" data frames, possibly with different column names, into a single "destination" data frame. Usually merge
will be faster and easier to implement if the columns to be merged on have the same names, and rbind
will always be faster and much easier if the column names and data types match exactly.
The key tool in this function is a "crosswalk" table (a data.frame
) that tells the function which fields in each source data frame match to the final fields in the destination data frame. Values in source data frame fields can be used as-is, combined across fields, or have functions applied to them before they are put into the destination data frame. If a source data frame doe snot gave a field that matches the destination field, a default value (including NA
) can be assigned to all cells for that source data frame.
The data frames to be combined can be provided in ...
or as file names in the first column of the crosswalk table. These can be either CSV files (extension ".csv
"), TAB files (extension ".tab
"), "Rdata" files (read using load
and with a ".rda
" or ".rdata
" extension), or "RDS" files (read using readRDS
and with a ".rds
" extension). The file type will be intuited from the extension, and its case does not matter. Note that if an object in an Rdata file has the same name as an object in this function (i.e., any of the arguments plus any objects internal to the function), this may cause a conflict. To help obviate this issue, all internal objects are named with a period at the end (e.g., "crossCell.
" and "countDf.
").
All cells in each source data frame will have leading and trailing white spaces removed before combining.
combineDf(
...,
crosswalk,
collapse = "; ",
useColumns = NULL,
excludeColumns = NULL,
useFrames = NULL,
classes = NULL,
verbose = FALSE
)
... |
Data frames to combine. These must be listed in the order that they appear in the |
crosswalk |
A Other than this column, the elements of each cell contain the name of the column in each source data frame that coincides with the column name in the More complex operations can be done using the following in cells of
|
collapse |
Character, specifies the string to put between fields combined with the |
useColumns , excludeColumns |
Logical, character vector, integer vector, or |
useFrames |
Logical, character, or |
classes |
Character or character list, specifies the classes (e.g., character, logical, numeric, integer) to be assigned to each column in the output table. If |
verbose |
Logical, if |
A data frame.
merge
, rbind
df1 <- data.frame(x1=1:5, x2=FALSE, x3=letters[1:5], x4=LETTERS[1:5],
x5='stuff', x6=11:15)
df2 <- data.frame(y1=11:15, y2=rev(letters)[1:5], y3=runif(5))
crosswalk <- data.frame(
a = c('x1', 'y1'),
b = c('x2', '%fill% TRUE'),
c = c('%cat% x3 x4', 'y2'),
d = c('x5', '%fill% NA'),
e = c('%fun% as.numeric(x6) > 12', '%fun% round(as.numeric(y3), 2)')
)
combined <- combineDf(df1, df2, crosswalk=crosswalk)
combined
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.