Generate a Codebook of a Data Set

Share:

Description

Function codebook collects documentation about an item, or the items in a data set or external data file. It returns an object that, when shown, print this documentation in a nicely formatted way.

Usage

1
2
3
4
5
codebook(x)
## S4 method for signature 'data.set'
codebook(x)
## S4 method for signature 'importer'
codebook(x)

Arguments

x

an atomic vector, factor, data frame, item, data.set or importer object.

Value

An object of class "codebook", for which a show method exists that produces a nicely formatted output. To export the formatted codebook into a text file one can use as.character in conjunction with writeLines.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Data <- data.set(
          vote = sample(c(1,2,3,8,9,97,99,NA),size=300,replace=TRUE),
          region = sample(c(rep(1,3),rep(2,2),3,99,NA),size=300,replace=TRUE),
          income = exp(rnorm(300,sd=.7))*2000
          )

Data <- within(Data,{
  description(vote) <- "Vote intention"
  description(region) <- "Region of residence"
  description(income) <- "Household income"
  wording(vote) <- "If a general election would take place next tuesday,
                    the candidate of which party would you vote for?"
  wording(income) <- "All things taken into account, how much do all
                    household members earn in sum?"
  Foreach(x=c(vote,region),{
    measurement(x) <- "nominal"
    })
  measurement(income) <- "ratio"
  labels(vote) <- c(
                    Conservatives         =  1,
                    Labour                =  2,
                    "Liberal Democrats"   =  3,
                    "Don't know"          =  8,
                    "Answer refused"      =  9,
                    "Not applicable"      = 97,
                    "Not asked in survey" = 99)
  labels(region) <- c(
                    England               =  1,
                    Scotland              =  2,
                    Wales                 =  3,
                    "Not applicable"      = 97,
                    "Not asked in survey" = 99)
  Foreach(x=c(vote,region,income),{
    annotation(x)["Remark"] <- "This is not a real survey item, of course ..."
    })
  missing.values(vote) <- c(8,9,97,99)
  missing.values(region) <- c(97,99)
})


codebook(Data)

codebook(iris)

codebook(within(iris,{
    Species[1:2]<-NA
    oSpecies <- ordered(Species)
  }))

## Not run: 
# Write the codebook of a data set to a file:
writeLines(as.character(codebook(Data)),
  con="Data-cdbk.txt")

## End(Not run)