Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
legTools
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
leg
legTools
Commits
3a80cc60
Commit
3a80cc60
authored
9 years ago
by
Walmes Zeviani
Browse files
Options
Downloads
Patches
Plain Diff
Add equalizeLevels to make 2 data.frames equal in its factors.
parent
913472a3
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
R/equalizeLevels.R
+54
-0
54 additions, 0 deletions
R/equalizeLevels.R
with
54 additions
and
0 deletions
R/equalizeLevels.R
0 → 100644
+
54
−
0
View file @
3a80cc60
#' @title Make that to data frames have the same factor levels
#'
#' @name equalizeLevels
#'
#' @description If two data frames have factor or character columns with
#' the same name, those in the first will have the same level order
#' as those in the second. So, in terms of factor columns, these
#' data frames will have the same levels in the same
#' order. Character columns in the first will be converted to factor
#' if they aren't. This function is useful to assing to the data
#' frame returned in the \code{grid} attribute returned by
#' \code{doBy::LSmeans()} or \code{doBy::LSmatrix()} the same order
#' to the levels present in the data frame used to fit the model and
#' estimate the parameters.
#'
#' @param \code{target} the target data frame that will have factor
#' levels reordered.
#' @param \code{ref} the reference data frame that contains the desired
#' level order.
#'
#' @return the first data data frame with the levels in a new order.
#'
#' @seealso \link[doBy]{LSmeans}, \link[doBy]{LSmatrix}.
#'
#' @author Walmes Zeviani, \email{walmes@@ufpr.br}
#'
#' @export
#'
#' @examples
#'
#'
equalizeLevels
<-
function
(
target
,
ref
){
if
(
is.data.frame
(
target
)
&
is.data.frame
(
ref
)){
com
<-
intersect
(
names
(
target
),
names
(
ref
))
for
(
i
in
com
){
if
(
!
is.null
(
levels
(
ref
[,
i
]))){
target
[,
i
]
<-
factor
(
target
[,
i
],
levels
=
levels
(
ref
[,
i
]))
}
}
target
}
else
stop
(
"`target` and `ref` must be a data.frame."
)
}
a
<-
data.frame
(
Species
=
as.character
(
sample
(
iris
$
Species
,
size
=
10
,
replace
=
TRUE
)),
stringsAsFactors
=
FALSE
)
str
(
a
)
levels
(
a
$
Species
)
levels
(
iris
$
Species
)
b
<-
equalizeLevels
(
target
=
a
,
ref
=
iris
)
str
(
b
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment