Debian Codegraph @

man/codeplan.Rd

raw
1 \name{codeplan}
2 \alias{codeplan}
3 \alias{codeplan,ANY-method}
4 \alias{codeplan,item.list-method}
5 \alias{codeplan,item-method}
6 \alias{codeplan<-}
7 \alias{setCodeplan}
8 \alias{setCodeplan,data.frame,codeplan-method}
9 \alias{setCodeplan,data.frame,NULL-method}
10 \alias{setCodeplan,data.set,codeplan-method}
11 \alias{setCodeplan,data.set,NULL-method}
12 \alias{setCodeplan,item,codeplan-method}
13 \alias{setCodeplan,item,NULL-method}
14 \alias{setCodeplan,atomic,codeplan-method}
15 \alias{setCodeplan,atomic,NULL-method}
16 \alias{read_codeplan}
17 \alias{write_codeplan}
18 \title{Describe structure of Data Sets and Importers}
19 \description{
20 The function \code{codeplan()} creates a data frame that
21 describes the structure of an item list (a \code{data.set} object or
22 an \code{importer} object), so that this structure can be stored and
23 and recovered. The resulting data frame has a particular print method
24 that delimits the output to one line per variable.
25
26 With \code{setCodeplan} an item list structure (as returned by \code{codeplan()})
27 can be applied to a data frame or data set. It is also possible to use an
28 assignment like \code{codeplan(x) <- value} to a similar effect.
29 }
30 \usage{
31 codeplan(x)
32 \S4method{codeplan}{item.list}(x)
33 \S4method{codeplan}{item}(x)
34 setCodeplan(x,value)
35 \S4method{setCodeplan}{data.frame,codeplan}(x,value)
36 \S4method{setCodeplan}{data.frame,NULL}(x,value)
37 \S4method{setCodeplan}{data.set,codeplan}(x,value)
38 \S4method{setCodeplan}{data.set,NULL}(x,value)
39 \S4method{setCodeplan}{item,codeplan}(x,value)
40 \S4method{setCodeplan}{item,NULL}(x,value)
41 \S4method{setCodeplan}{atomic,codeplan}(x,value)
42 \S4method{setCodeplan}{atomic,NULL}(x,value)
43 codeplan(x) <- value
44 read_codeplan(filename,type)
45 write_codeplan(x,filename,type,pretty)
46 }
47 \arguments{
48 \item{x}{for \code{codeplan(x)} an object that inherits from class \code{"item.list"},
49 i.e. can be a \code{"data.set"} object or an \code{"importer"}
50 object, it can also be an object that inherits from class
51 \code{"item"}.
52 For \code{write_codeplan} an object from class \code{"codeplan"}.}
53 \item{value}{an object as it would be returned by \code{codeplan(x)}
54 or \code{NULL}.}
55 \item{filename}{a character string, the name of the file that is to be
56 read or to be written.}
57 \item{type}{a character string (either "yaml" or "json") oder NULL (the default), gives the type
58 of the file into which the codeplan is written or from
59 which it is read.
60 If \code{type} is NULL then the file type is inferred from
61 the file name ending (".yaml" or ",yml" for "yaml",
62 ".json" for "json").
63 }
64 \item{pretty}{a logical value, whether the JSON output created by \code{write_codeplan(...)} should be prettified.}
65 }
66 \value{If applicable, \code{codeplan} returns a list with
67 additional S3 class attribute \code{"codeplan"}. For arguments for
68 which the relevant information does not exist, the function returns \code{NULL}.
69
70 The list has at least one element or several elements, named after the
71 variable in the "item.list" or "data.set" \code{x}. Each list element
72 is a list itself with the following elements:
73
74 \item{\code{annotation}}{a named character vector,}
75 \item{\code{labels}}{a named list of labels and labelled values}
76 \item{\code{value.filter}}{a list with at least two elements named
77 "class" and "filter", and optionally another element named
78 "range". The "class" element determines the class of the
79 value filter and equals either "missing.values", "valid.values",
80 or "valid.range". An element named "range" may only be needed
81 if "class" is "missing.values", as it is possible (like in SPSS)
82 to have \emph{both} individual missing values and a range of
83 missing values.
84 }
85 \item{\code{mode}}{a character string that describes storage mode, such as \code{"character"},
86 \code{"integer"}, or \code{"numeric"}.}
87 \item{\code{measurement}}{a character string with the measurement level,
88 \code{"nominal"}, \code{"ordinal"}, \code{"interval"}, or
89 \code{"ratio"}.}
90
91 If \code{codeplan(x)<-value} or \code{setCodeplan(x,value)} is used
92 and \code{value} is \code{NULL}, all the special information about
93 annotation, labels, value filters, etc. is removed from the resulting
94 object, which then is usually a mere atomic vector or data frame.
95 }
96 \examples{
97 Data1 <- data.set(
98 vote = sample(c(1,2,3,8,9,97,99),size=300,replace=TRUE),
99 region = sample(c(rep(1,3),rep(2,2),3,99),size=300,replace=TRUE),
100 income = exp(rnorm(300,sd=.7))*2000
101 )
102
103 Data1 <- within(Data1,{
104 description(vote) <- "Vote intention"
105 description(region) <- "Region of residence"
106 description(income) <- "Household income"
107 foreach(x=c(vote,region),{
108 measurement(x) <- "nominal"
109 })
110 measurement(income) <- "ratio"
111 labels(vote) <- c(
112 Conservatives = 1,
113 Labour = 2,
114 "Liberal Democrats" = 3,
115 "Don't know" = 8,
116 "Answer refused" = 9,
117 "Not applicable" = 97,
118 "Not asked in survey" = 99)
119 labels(region) <- c(
120 England = 1,
121 Scotland = 2,
122 Wales = 3,
123 "Not applicable" = 97,
124 "Not asked in survey" = 99)
125 foreach(x=c(vote,region,income),{
126 annotation(x)["Remark"] <- "This is not a real survey item, of course ..."
127 })
128 missing.values(vote) <- c(8,9,97,99)
129 missing.values(region) <- c(97,99)
130 })
131 cpData1 <- codeplan(Data1)
132
133 Data2 <- data.frame(
134 vote = sample(c(1,2,3,8,9,97,99),size=300,replace=TRUE),
135 region = sample(c(rep(1,3),rep(2,2),3,99),size=300,replace=TRUE),
136 income = exp(rnorm(300,sd=.7))*2000
137 )
138 codeplan(Data2) <- cpData1
139 codeplan(Data2)
140 codebook(Data2)
141
142 # Note the difference between 'as.data.frame' and setting
143 # the codeplan to NULL:
144 Data2df <- as.data.frame(Data2)
145 codeplan(Data2) <- NULL
146 str(Data2)
147 str(Data2df)
148 codeplan(Data2) <- NULL # Does not change anything
149
150 # Codeplans of survey items can also be inquired and manipulated:
151 vote <- Data1$vote
152 str(vote)
153 cp.vote <- codeplan(vote)
154 codeplan(vote) <- NULL
155 str(vote)
156 codeplan(vote) <- cp.vote
157 vote
158
159 fn.json <- paste0(tempfile(),".json")
160 write_codeplan(codeplan(Data1),filename=fn.json)
161 codeplan(Data2) <- read_codeplan(fn.json)
162 codeplan(Data2)
163 }