-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new 'deduplicate_labels()' function.
- Loading branch information
Showing
6 changed files
with
162 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
combine_duplicated_labels <- function(x){ | ||
dl <- duplicated_labels(x) | ||
l <- labels(x) | ||
for(i in seq_along(dl)){ | ||
old_val <- dl[[i]] | ||
new_val <- old_val[1] | ||
drop_val <- old_val[-1] | ||
x[x %in% old_val] <- new_val | ||
drop <- l@values %in% drop_val | ||
l@.Data <- l@.Data[!drop] | ||
l@values <- l@values[!drop] | ||
labels(x) <- l | ||
} | ||
x | ||
} | ||
|
||
|
||
prefix_duplicated_labels <- function(x, | ||
pattern="%d. %s", | ||
...){ | ||
dl <- duplicated_labels(x) | ||
if(length(dl)){ | ||
l <- labels(x) | ||
dedup_lab <- sprintf(pattern,l@values,l@.Data) | ||
l@.Data <- dedup_lab | ||
labels(x) <- l | ||
} | ||
x | ||
} | ||
|
||
|
||
postfix_duplicated_labels <- function(x, | ||
pattern="%s (%d)", | ||
...){ | ||
dl <- duplicated_labels(x) | ||
l <- labels(x) | ||
for(i in seq_along(dl)){ | ||
dup_lab <- names(dl)[i] | ||
dup_val <- dl[[i]] | ||
ii <- match(dup_val,l@values) | ||
dedup_lab <- sprintf(pattern,dup_lab,dup_val) | ||
l@.Data[ii] <- dedup_lab | ||
} | ||
labels(x) <- l | ||
x | ||
} | ||
|
||
deduplicate_labels <- function(x,...) UseMethod("deduplicate_labels") | ||
deduplicate_labels.default <- function(x,...) return(x) | ||
|
||
deduplicate_labels.item <- function(x,method=c("combine codes", | ||
"prefix values", | ||
"postfix values"),...){ | ||
method <- match.arg(method) | ||
# browser() | ||
switch(method, | ||
"combine codes"=combine_duplicated_labels(x), | ||
"prefix values"=prefix_duplicated_labels(x,...), | ||
"postfix values"=postfix_duplicated_labels(x,...)) | ||
} | ||
|
||
deduplicate_labels.item.list <- function(x,...){ | ||
n <- ncol(x) | ||
for(i in 1:n){ | ||
x.i <- x[[i]] | ||
if(length(labels(x.i)) && | ||
length(duplicated_labels(x.i))){ | ||
x.i <- deduplicate_labels(x.i,...) | ||
x@.Data[[i]] <- x.i | ||
} | ||
} | ||
x | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
\name{deduplicate_labels} | ||
\alias{deduplicate_labels} | ||
\alias{deduplicate_labels.item} | ||
\alias{deduplicate_labels.item.list} | ||
\title{Handle duplicated labels} | ||
\description{ | ||
The function \code{deduplicate_labels} can be used with "item" objects, | ||
"importer" objects or "data.set" objects to deal with | ||
duplicate labels, | ||
i.e. labels that are attached to more than | ||
one code. There are several ways to de-duplicate labels: by combining | ||
values that share their label or by making labels duplicate labels distinct. | ||
} | ||
\usage{ | ||
deduplicate_labels(x,\dots) | ||
\S3method{deduplicate_labels}{item}(x, | ||
method=c("combine codes", | ||
"prefix values", | ||
"postfix values"),\dots) | ||
# Applicable to 'importer' objects and 'data.set' objects | ||
\S3method{deduplicate_labels}{item.list}(x,\dots) | ||
} | ||
\arguments{ | ||
\item{x}{an item with value labels or that contains items with | ||
value labels} | ||
\item{method}{a character string that determines the method to | ||
make value labels unique.} | ||
\item{\dots}{other arguments, passed to specific methods of the | ||
generic function.} | ||
} | ||
\value{The function \code{deduplicate_labels} a copy of \code{x} | ||
that has unqiue value labels. | ||
} | ||
\examples{ | ||
x1 <- as.item(rep(1:5,4), | ||
labels=c( | ||
A = 1, | ||
A = 2, | ||
B = 3, | ||
B = 4, | ||
C = 5 | ||
), | ||
annotation = c( | ||
description="Yet another test" | ||
)) | ||
|
||
x2 <- as.item(rep(1:4,5), | ||
labels=c( | ||
i = 1, | ||
ii = 2, | ||
iii = 3, | ||
iii = 4 | ||
), | ||
annotation = c( | ||
description="Still another test" | ||
)) | ||
|
||
x3 <- as.item(rep(1:2,10), | ||
labels=c( | ||
a = 1, | ||
b = 2 | ||
), | ||
annotation = c( | ||
description="Still another test" | ||
)) | ||
|
||
codebook(deduplicate_labels(x1)) | ||
codebook(deduplicate_labels(x1,method="prefix")) | ||
codebook(deduplicate_labels(x1,method="postfix")) | ||
|
||
ds <- data.set(x1,x2,x3) | ||
codebook(deduplicate_labels(ds)) | ||
codebook(deduplicate_labels(ds,method="prefix")) | ||
codebook(deduplicate_labels(ds,method="postfix")) | ||
|
||
} |