Learn R Programming

memisc (version 0.99.31.8)

codebook: Generate a Codebook of a Data Set

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

codebook(x, weights = NULL, unweighted = TRUE, ...)
# S4 method for item
codebook(x, weights = NULL, unweighted = TRUE, ...)
# S4 method for atomic
codebook(x, weights = NULL, unweighted = TRUE, ...)
# S4 method for factor
codebook(x, weights = NULL, unweighted = TRUE, ...)
# S4 method for data.set
codebook(x, weights = NULL, unweighted = TRUE, ...)
# S4 method for importer
codebook(x, weights = NULL, unweighted = TRUE, ...)
# S4 method for data.frame
codebook(x, weights = NULL, unweighted = TRUE, ...)
# S4 method for tbl_df
codebook(x, weights = NULL, unweighted = TRUE, ...)

Value

An object of class "codebook", for which a show method exists that produces a nicely formatted output.

Arguments

x

an item, numeric or character vector, factor, data.set, data.frame or importer object for codebook()

weights

an optional vector of weights.

unweighted

an optional logical vector; if weights are given, it determines of only summaries of weighted data are show or also summaries of unweighted data.

...

other arguments, currently ignored.

Examples

Run this code

Data <- data.set(
          vote = sample(c(1,2,3,8,9,97,99),size=300,replace=TRUE),
          region = sample(c(rep(1,3),rep(2,2),3,99),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)
})

description(Data)

codebook(Data)

codebook(Data)$vote
codebook(Data)[2]

codebook(Data[2])
 
DataFr <- as.data.frame(Data)
DataHv <- as_haven(Data,user_na=TRUE)

codebook(DataFr)
codebook(DataHv)

   
if (FALSE) {
Write(description(Data),
           file="Data-desc.txt")
Write(codebook(Data),
           file="Data-cdbk.txt")
  }

Run the code above in your browser using DataLab