| 1 |
\name{data.set} |
| 2 |
\alias{data.set} |
| 3 |
\alias{data.set-class} |
| 4 |
\alias{initialize,data.set-method} |
| 5 |
\alias{is.data.set} |
| 6 |
\alias{within,data.set-method} |
| 7 |
\alias{print,data.set-method} |
| 8 |
\alias{format,data.set-method} |
| 9 |
\alias{show,data.set-method} |
| 10 |
\alias{summary,data.set-method} |
| 11 |
\alias{as.data.frame.data.set} |
| 12 |
\alias{str.data.set} |
| 13 |
\alias{as.data.set} |
| 14 |
\alias{as.data.set,list-method} |
| 15 |
\alias{[<-,data.set-method} |
| 16 |
\alias{[,data.set,atomic,atomic,ANY-method} |
| 17 |
\alias{[,data.set,atomic,missing,ANY-method} |
| 18 |
\alias{[,data.set,missing,atomic,ANY-method} |
| 19 |
\alias{[,data.set,missing,missing,ANY-method} |
| 20 |
\alias{dim,data.set-method} |
| 21 |
\alias{dimnames,data.set-method} |
| 22 |
\alias{dimnames<-,data.set-method} |
| 23 |
\alias{row.names,data.set-method} |
| 24 |
\alias{head,data.set-method} |
| 25 |
\alias{tail,data.set-method} |
| 26 |
\alias{tail,data.set-method} |
| 27 |
\alias{as.data.table.data.set} |
| 28 |
\alias{dsView} |
| 29 |
\title{Data Set Objects} |
| 30 |
\description{ |
| 31 |
\code{"data.set"} objects are collections of \code{"item"} objects, |
| 32 |
with similar semantics as data frames. They are distinguished |
| 33 |
from data frames so that coercion by \code{as.data.fame} |
| 34 |
leads to a data frame that contains only vectors and factors. |
| 35 |
Nevertheless most methods for data frames are inherited by |
| 36 |
data sets, except for the method for the \code{within} generic |
| 37 |
function. For the \code{within} method for data sets, see the details section. |
| 38 |
|
| 39 |
Thus data preparation using data sets retains all informations |
| 40 |
about item annotations, labels, missing values etc. |
| 41 |
While (mostly automatic) conversion of data sets into data |
| 42 |
frames makes the data amenable for the use of R's statistical |
| 43 |
functions. |
| 44 |
|
| 45 |
\code{dsView} is a function that displays data sets in a similar |
| 46 |
manner as \code{View} displays data frames. (\code{View} works |
| 47 |
with data sets as well, but changes them first into data frames.) |
| 48 |
} |
| 49 |
\usage{ |
| 50 |
data.set(\dots,row.names = NULL, check.rows = FALSE, check.names = TRUE, |
| 51 |
stringsAsFactors = FALSE, document = NULL) |
| 52 |
as.data.set(x, row.names=NULL, \dots) |
| 53 |
\S4method{as.data.set}{list}(x,row.names=NULL,\dots) |
| 54 |
is.data.set(x) |
| 55 |
\method{as.data.frame}{data.set}(x, row.names = NULL, optional = FALSE, \dots) |
| 56 |
\S4method{within}{data.set}(data, expr, \dots) |
| 57 |
|
| 58 |
dsView(x) |
| 59 |
|
| 60 |
\S4method{head}{data.set}(x,n=20,\dots) |
| 61 |
\S4method{tail}{data.set}(x,n=20,\dots) |
| 62 |
} |
| 63 |
\arguments{ |
| 64 |
\item{\dots}{ |
| 65 |
For the \code{data.set} function several vectors or items, |
| 66 |
for \code{within} further, ignored arguments. |
| 67 |
} |
| 68 |
\item{row.names, check.rows, check.names, stringsAsFactors, optional}{arguments |
| 69 |
as in \code{\link[base]{data.frame}} or \code{\link[base]{as.data.frame}}, |
| 70 |
respectively. |
| 71 |
} |
| 72 |
\item{document}{NULL or an optional character vector that contains |
| 73 |
documenation of the data.} |
| 74 |
\item{x}{for \code{is.data.set(x)}, any object; for |
| 75 |
\code{as.data.frame(x,...)} and \code{dsView(x)} a "data.set" object.} |
| 76 |
\item{data}{a data set, that is, an object of class "data.set". } |
| 77 |
\item{expr}{an expression, or several expressions enclosed in curly braces.} |
| 78 |
\item{n}{integer; the number of rows to be shown by \code{head} or \code{tail}} |
| 79 |
} |
| 80 |
\details{ |
| 81 |
The \code{as.data.frame} method for data sets is just a copy |
| 82 |
of the method for list. Consequently, all items in the data set |
| 83 |
are coerced in accordance to their \code{\link{measurement}} setting, |
| 84 |
see \code{\link{as.vector,item-method}} and \code{\link{measurement}}. |
| 85 |
|
| 86 |
The \code{within} method for data sets has the same effect as |
| 87 |
the \code{\link[base:with]{within}} method for data frames, apart from two differences: |
| 88 |
all results of the computations are coerced into items if |
| 89 |
they have the appropriate length, otherwise, they are automatically |
| 90 |
dropped. |
| 91 |
|
| 92 |
Currently only one method for the generic function \code{as.data.set} |
| 93 |
is defined: a method for "importer" objects. |
| 94 |
} |
| 95 |
\value{ |
| 96 |
\code{data.set} and the \code{within} method for |
| 97 |
data sets returns a "data.set" object, \code{is.data.set} |
| 98 |
returns a logical value, and \code{as.data.frame} returns |
| 99 |
a data frame. |
| 100 |
} |
| 101 |
\examples{ |
| 102 |
Data <- data.set( |
| 103 |
vote = sample(c(1,2,3,8,9,97,99),size=300,replace=TRUE), |
| 104 |
region = sample(c(rep(1,3),rep(2,2),3,99),size=300,replace=TRUE), |
| 105 |
income = exp(rnorm(300,sd=.7))*2000 |
| 106 |
) |
| 107 |
|
| 108 |
Data <- within(Data,{ |
| 109 |
description(vote) <- "Vote intention" |
| 110 |
description(region) <- "Region of residence" |
| 111 |
description(income) <- "Household income" |
| 112 |
wording(vote) <- "If a general election would take place next tuesday, |
| 113 |
the candidate of which party would you vote for?" |
| 114 |
wording(income) <- "All things taken into account, how much do all |
| 115 |
household members earn in sum?" |
| 116 |
foreach(x=c(vote,region),{ |
| 117 |
measurement(x) <- "nominal" |
| 118 |
}) |
| 119 |
measurement(income) <- "ratio" |
| 120 |
labels(vote) <- c( |
| 121 |
Conservatives = 1, |
| 122 |
Labour = 2, |
| 123 |
"Liberal Democrats" = 3, |
| 124 |
"Don't know" = 8, |
| 125 |
"Answer refused" = 9, |
| 126 |
"Not applicable" = 97, |
| 127 |
"Not asked in survey" = 99) |
| 128 |
labels(region) <- c( |
| 129 |
England = 1, |
| 130 |
Scotland = 2, |
| 131 |
Wales = 3, |
| 132 |
"Not applicable" = 97, |
| 133 |
"Not asked in survey" = 99) |
| 134 |
foreach(x=c(vote,region,income),{ |
| 135 |
annotation(x)["Remark"] <- "This is not a real survey item, of course ..." |
| 136 |
}) |
| 137 |
missing.values(vote) <- c(8,9,97,99) |
| 138 |
missing.values(region) <- c(97,99) |
| 139 |
|
| 140 |
# These to variables do not appear in the |
| 141 |
# the resulting data set, since they have the wrong length. |
| 142 |
junk1 <- 1:5 |
| 143 |
junk2 <- matrix(5,4,4) |
| 144 |
|
| 145 |
}) |
| 146 |
# Since data sets may be huge, only a |
| 147 |
# part of them are 'show'n |
| 148 |
Data |
| 149 |
|
| 150 |
\dontrun{ |
| 151 |
|
| 152 |
# If we insist on seeing all, we can use 'print' instead |
| 153 |
print(Data) |
| 154 |
} |
| 155 |
|
| 156 |
str(Data) |
| 157 |
summary(Data) |
| 158 |
|
| 159 |
\dontrun{ |
| 160 |
# If we want to 'View' a data set we can use 'dsView' |
| 161 |
dsView(Data) |
| 162 |
# Works also, but changes the data set into a data frame first: |
| 163 |
View(Data) |
| 164 |
} |
| 165 |
|
| 166 |
Data[[1]] |
| 167 |
Data[1,] |
| 168 |
head(as.data.frame(Data)) |
| 169 |
|
| 170 |
EnglandData <- subset(Data,region == "England") |
| 171 |
EnglandData |
| 172 |
|
| 173 |
xtabs(~vote+region,data=Data) |
| 174 |
xtabs(~vote+region,data=within(Data, vote <- include.missings(vote))) |
| 175 |
} |