-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Renamed 'reverse_codes()' to 'reversed()'.
- Loading branch information
Showing
7 changed files
with
90 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
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 was deleted.
Oops, something went wrong.
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,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) |
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 was deleted.
Oops, something went wrong.
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,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) | ||
} |