Skip to content

Commit

Permalink
Renamed 'reverse_codes()' to 'reversed()'.
Browse files Browse the repository at this point in the history
  • Loading branch information
melff committed Feb 14, 2021
1 parent c01899b commit 0292ac9
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 70 deletions.
2 changes: 1 addition & 1 deletion pkg/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: memisc
Type: Package
Title: Management of Survey Data and Presentation of Analysis Results
Version: 0.99.29
Date: 2021-01-25
Date: 2021-02-14
Author: Martin Elff (with contributions from Christopher N. Lawrence, Dave Atkins, Jason W. Morgan, Achim Zeileis)
Maintainer: Martin Elff <[email protected]>
Description: An infrastructure for the management of survey data including
Expand Down
2 changes: 1 addition & 1 deletion pkg/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -483,4 +483,4 @@ S3method(deduplicate_labels,item.list)

export(trim_labels)
exportMethods(trim_labels)
export(reverse_codes)
export(reversed)
37 changes: 0 additions & 37 deletions pkg/R/reorder-codes.R

This file was deleted.

42 changes: 42 additions & 0 deletions pkg/R/reversed.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
reversed.item <- function(x){
y <- x
if(length(labels(x))){
l <- labels(x)
vals <- vals.orig <- l@values
labs <- l@.Data
vf <- value.filter(x)
from <- vals[is.valid2(vals,vf)]
to <- rev(from)
for(i in seq_along(from)){
y[x==from[i]] <- to[i]
vals[vals.orig==from[i]] <- to[i]
}
to.l <- structure(vals,names=labs)
ii <- order(to.l)
to.l <- to.l[ii]
labels(y) <- to.l
} else {
v <- valid.values(x)
if(length(v)){
from <- v@filter
to <- rev(from)
for(i in seq_along(from)){
y[x==from[i]] <- to[i]
}
} else stop("No way to determine how reverse the codes.")
}
return(y)
}




reversed.factor <- function(x){
y <- factor(x,levels=rev(levels(x)))
return(y)
}

setGeneric("reversed",
function(x)standardGeneric("reversed"))
setMethod("reversed","item.vector",reversed.item)
setMethod("reversed","factor",reversed.factor)
4 changes: 4 additions & 0 deletions pkg/inst/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2021-02-14:
- Renamed 'reverse_codes()' to 'reversed()', which now leaves missing
values unchanged.

2021-01-25:
- Made function 'reorder_codes()' redundant and memoved it.

Expand Down
31 changes: 0 additions & 31 deletions pkg/man/reorder-codes.Rd

This file was deleted.

42 changes: 42 additions & 0 deletions pkg/man/reversed.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
\name{reversed}
\alias{reversed}
\alias{reversed,item.vector-method}
\alias{reversed,factor-method}
\title{Reverse the codes of a survey item or the levels of a factor}
\description{
The function \code{reversed()} returns a copy of its argument with codes
or levels in reverse order.
}
\usage{
reversed(x)
\S4method{reversed}{item.vector}(x)
\S4method{reversed}{factor}(x)
}
\arguments{
\item{x}{An object -- an "item" object or a "data.set" object}
}
\value{
If the argument of the function \code{reversed()} than either the
unique valid values or the labelled valid values recoded into the
reverse order.

If th argument is a factor than the function returns the factor with
levels in reverse order.
}
\examples{
ds <- data.set(
x = as.item(sample(c(1:3,9),100,replace=TRUE),
labels=c("One"=1,
"Two"=2,
"Three"=3,
"Missing"=9)))
df <- as.data.frame(ds)
ds <- within(ds,{
xr <- reversed(x)
})
codebook(ds)
df <- within(df,{
xr <- reversed(x)
})
codebook(df)
}

0 comments on commit 0292ac9

Please sign in to comment.