| 1 |
\name{items-to-vectors} |
| 2 |
\alias{as.data.frame.character.item} |
| 3 |
\alias{as.data.frame.double.item} |
| 4 |
\alias{as.data.frame.integer.item} |
| 5 |
\alias{as.data.frame.datetime.item} |
| 6 |
\alias{as.data.frame.Date.item} |
| 7 |
\alias{as.vector,item-method} |
| 8 |
\alias{as.numeric,item-method} |
| 9 |
\alias{as.integer,item-method} |
| 10 |
\alias{as.factor,item.vector-method} |
| 11 |
\alias{as.ordered,item.vector-method} |
| 12 |
\alias{as.character,item.vector-method} |
| 13 |
\alias{as.character,datetime.item-method} |
| 14 |
\alias{as.character,Date.item-method} |
| 15 |
\alias{as.character,datetime.item.vector-method} |
| 16 |
\alias{as.character,Date.item.vector-method} |
| 17 |
|
| 18 |
\title{How Survey Items Are Converted into "Ordinary" Data Vectors} |
| 19 |
\description{ |
| 20 |
Survey item objects in are numeric or character vectors with some extra information |
| 21 |
that may helpful for for managing and documenting survey data, but they are not suitable |
| 22 |
for statistical data analysis. To run regressions etc. one should convert |
| 23 |
\code{\link{item}} objects into "ordinary" numeric vectors or factors. |
| 24 |
This means that codes or values declared as "missing" (if present) are translated into |
| 25 |
the generial missing value \code{NA}, while value labels (if defined) are translated into |
| 26 |
factor levels. |
| 27 |
} |
| 28 |
\usage{ |
| 29 |
# The following methods can be used to covert items into |
| 30 |
# vectors with a given mode or into factors. |
| 31 |
\S4method{as.vector}{item}(x, mode = "any") |
| 32 |
\S4method{as.numeric}{item}(x, \dots) |
| 33 |
\S4method{as.integer}{item}(x, \dots) |
| 34 |
\S4method{as.factor}{item.vector}(x) |
| 35 |
\S4method{as.ordered}{item.vector}(x) |
| 36 |
\S4method{as.character}{item.vector}(x, use.labels = TRUE, include.missings = FALSE, \dots) |
| 37 |
\S4method{as.character}{datetime.item.vector}() |
| 38 |
\S4method{as.character}{Date.item.vector}() |
| 39 |
# The following methods are unlikely to be useful in practice, other than |
| 40 |
# that they are called internally by the 'as.data.frame()' method for "data.set" |
| 41 |
# objects. |
| 42 |
\method{as.data.frame}{character.item}(x, row.names = NULL, optional = FALSE, \dots) |
| 43 |
\method{as.data.frame}{double.item}(x, row.names = NULL, optional = FALSE, \dots) |
| 44 |
\method{as.data.frame}{integer.item}(x, row.names = NULL, optional = FALSE, \dots) |
| 45 |
\method{as.data.frame}{Date.item}(x, row.names = NULL, optional = FALSE, \dots) |
| 46 |
\method{as.data.frame}{datetime.item}(x, row.names = NULL, optional = FALSE, \dots) |
| 47 |
} |
| 48 |
|
| 49 |
\arguments{ |
| 50 |
\item{x}{an object in class "item","item.vector", etc., as relevant |
| 51 |
for the respective conversion method.} |
| 52 |
\item{mode}{the mode of the vector to be returned, usually \code{"numeric"}, |
| 53 |
\code{"integer"}, or \code{"charcater"}} |
| 54 |
\item{use.labels}{logical,should value labels be used for creating |
| 55 |
the character vector?} |
| 56 |
\item{include.missings}{logical; if \code{TRUE}, declared missing values are |
| 57 |
not converted into \code{NA}, but into character strings with \code{"*"} as the "missingness marker" |
| 58 |
added at the beginning.} |
| 59 |
\item{row.names}{optional row names, see \code{\link[base]{as.data.frame}}} |
| 60 |
\item{optional}{a logical value, see \code{\link[base]{as.data.frame}}} |
| 61 |
\item{\dots}{other arguments, ignored.} |
| 62 |
} |
| 63 |
|
| 64 |
\value{ |
| 65 |
The function \code{as.vector()} returns a logical, numeric, or character |
| 66 |
depending on the \code{mode=} argument. If \code{mode="any"}, the vector |
| 67 |
has the mode that corresponds to the (internal) mode of the item |
| 68 |
vector, that is, an item in class "integer.item" will become an integer |
| 69 |
vector, an item in class "double.item" will become a double-precision |
| 70 |
numeric vector, an item in class "character.item" will become a |
| 71 |
character vector; since the internal mode of a "dateitem.item" or a |
| 72 |
"Date.item" vector is numeric, a numeric vector will be returned. |
| 73 |
|
| 74 |
The functions \code{as.integer()}, \code{as.numeric()}, \code{as.character()}, |
| 75 |
\code{as.factor()}, and \code{as.ordered()} return an integer, numeric, |
| 76 |
or character vector, or an ordered or unordered factor, respectively. |
| 77 |
|
| 78 |
When \code{as.data.frame()} is applied to an survey item object, the |
| 79 |
result is a single-column data frame, where the single column is a |
| 80 |
numeric vector or character vector or factor depending on the |
| 81 |
\code{\link{measurement}} attribute of the item. In particular, if the |
| 82 |
\code{\link{measurement}} attribute equals \code{"ratio"} or |
| 83 |
\code{"interval"} this column will be the result of \code{as.vector()}, |
| 84 |
if the \code{\link{measurement}} attribute equals \code{"ordinal"} this |
| 85 |
column will be an ordered factor (see \code{\link{ordered}}), and if |
| 86 |
the \code{\link{measurement}} attribute equals \code{"nominal"} this |
| 87 |
column will be an unordered factor (see \code{\link{factor}}). |
| 88 |
|
| 89 |
All these functions have in common that values declared as "missing" by |
| 90 |
virtue of the \code{\link{value.filter}} attribute will be turned into \code{NA}. |
| 91 |
} |
| 92 |
|
| 93 |
\seealso{ |
| 94 |
\code{\link{items}} |
| 95 |
\code{\link{annotation}} |
| 96 |
\code{\link{labels}} |
| 97 |
\code{\link{value.filter}} |
| 98 |
} |
| 99 |
\examples{ |
| 100 |
x <- as.item(rep(1:5,4), |
| 101 |
labels=c( |
| 102 |
"First" = 1, |
| 103 |
"Second" = 2, |
| 104 |
"Third" = 3, |
| 105 |
"Fourth" = 4, |
| 106 |
"Don't know" = 5 |
| 107 |
), |
| 108 |
missing.values=5, |
| 109 |
annotation = c( |
| 110 |
description="test" |
| 111 |
)) |
| 112 |
str(x) |
| 113 |
summary(x) |
| 114 |
as.numeric(x) |
| 115 |
|
| 116 |
test <- as.item(rep(1:6,2),labels=structure(1:6, |
| 117 |
names=letters[1:6])) |
| 118 |
|
| 119 |
as.factor(test) |
| 120 |
as.numeric(test) |
| 121 |
as.character(test) |
| 122 |
as.character(test,include.missings=TRUE) |
| 123 |
|
| 124 |
as.data.frame(test)[[1]] |
| 125 |
} |
| 126 |
\keyword{manip} |