-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for objects from 'tibble' and 'haven'
- Loading branch information
Showing
6 changed files
with
190 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
setGeneric("prep_for_tibble",function(x,...)standardGeneric("prep_for_tibble")) | ||
setMethod("prep_for_tibble",signature(x="item.vector"),function(x,...) | ||
prep_for_tibble_item_vector(x)) | ||
|
||
prep_for_tibble_item_vector <- function(x,...){ | ||
y <- if(inherits(x,"character.item")) as.character(x) | ||
else if(is.ordinal(x)) as.ordered(x) | ||
else if(is.nominal(x)) as.factor(x) | ||
else as.vector(x) | ||
d <- description(x) | ||
if(length(d)) | ||
attr(y,"label") <- d | ||
return(y) | ||
} | ||
|
||
as_tibble.data.set <- function(x, ...){ | ||
y <- lapply(x@.Data, | ||
prep_for_tibble_item_vector) | ||
names(y) <- names(x) | ||
attr(y,"row.names") <- x@row_names | ||
class(y) <- c("tbl_df","tbl","data.frame") | ||
return(y) | ||
} | ||
|
||
setOldClass("haven_labelled") | ||
setMethod("as.item",signature(x="haven_labelled"),function(x,...){ | ||
annotation <- c(description=attr(x,"label")) | ||
labels <- attr(x,"labels") | ||
class(x) <- NULL | ||
as.item(x,labels=labels, | ||
annotation=annotation) | ||
}) | ||
|
||
setOldClass("haven_labelled_spss") | ||
setMethod("as.item",signature(x="haven_labelled_spss"),function(x,...){ | ||
annotation <- c(description=attr(x,"label")) | ||
labels <- attr(x,"labels") | ||
mis_range <- attr(x,"na_range") | ||
mis_values <- attr(x,"na_values") | ||
class(x) <- NULL | ||
value_filter <- new("missing.values", | ||
filter=mis_values, | ||
range=mis_range) | ||
as.item(x,labels=labels, | ||
annotation=annotation, | ||
value.filter=value_filter) | ||
}) | ||
|
||
setGeneric("as_haven",function(x,...)standardGeneric("as_haven")) | ||
setMethod("as_haven",signature(x="data.set"),function(x,user_na=FALSE,...){ | ||
y <- lapply(x@.Data,as_haven,user_na=user_na,...) | ||
names(y) <- names(x) | ||
attr(y,"row.names") <- x@row_names | ||
class(y) <- c("tbl_df","tbl","data.frame") | ||
return(y) | ||
}) | ||
setMethod("as_haven",signature(x="item.vector"),function(x,user_na=FALSE,...){ | ||
y <- x@.Data | ||
attr(y,"label") <- description(x) | ||
attr(y,"labels") <- as.vector(labels(x)) | ||
ms <- missing.values(x) | ||
if(user_na && length(ms)){ | ||
attr(y,"na_values") <- ms@filter | ||
attr(y,"na_range") <- ms@range | ||
class(y) <- "haven_labelled_spss" | ||
} else { | ||
ism <- is.missing(x) | ||
y[ism] <- NA | ||
class(y) <- "haven_labelled" | ||
} | ||
return(y) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
\name{tibbles} | ||
\alias{as_tibble.data.set} | ||
\alias{as.item,haven_labelled-method} | ||
\alias{as.item,haven_labelled_spss-method} | ||
\alias{as_haven} | ||
\alias{as_haven,data.set-method} | ||
\alias{as_haven,item.vector-method} | ||
\title{Interface to packages 'tibble' and 'haven'} | ||
\description{ | ||
A \code{as_tibble} method (\code{as_table.data.set}) allows to transform \code{"data.set"} objects | ||
into objects of class \code{"tbl_df"} as defined by the package | ||
"tibble". | ||
|
||
\code{as.item} methods for objects of classes \code{"haven_labelled"} | ||
and \code{"have_labelled_spss"} allow to transform a "tibble" imported | ||
using \code{read_dta}, \code{read_spss}, etc. from the package "haven" | ||
into an object of class \code{"data.set"}. | ||
|
||
\code{as_haven} can be used to transform \code{"data.set"} objects | ||
into objects of class \code{"tbl_df"} with that additional information | ||
that objects imported using the "haven" package usually have, i.e. | ||
variable labels and value labels (as the \code{"label"} and | ||
\code{"labels"} attributes of the columns). | ||
} | ||
\usage{ | ||
as_tibble.data.set(x,\dots) | ||
\S4method{as.item}{haven_labelled}(x,\dots) | ||
\S4method{as.item}{haven_labelled_spss}(x,\dots) | ||
as_haven(x,\dots) | ||
\S4method{as_haven}{data.set}(x,user_na=FALSE,\dots) | ||
\S4method{as_haven}{item.vector}(x,user_na=FALSE,\dots) | ||
} | ||
\arguments{ | ||
\item{x}{for \code{as_tibble.data.set} and \code{as_haven}, an object | ||
of class \code{"data.set"}; for \code{as.item}, an object of class | ||
\code{"haven_labelled"} or \code{"haven_labelled_spss"}.} | ||
\item{user_na}{logical; if \code{TRUE} then the resulting vectors | ||
have an \code{"na_values"} and/or \code{"na_range"} attribute.} | ||
\item{\dots}{further arguments, passed through to other the the | ||
\code{as_tibble} method for lists, or ignored.} | ||
} | ||
\value{ | ||
\code{as_tibble.data.set} and the \code{"data.set"}-method of | ||
\code{as_haven} return a "tibble". The \code{"item.vector"}-method | ||
(which is for internal use only) returns a vector with S3 class either | ||
\code{"haven_labelled"} or \code{"haven_labelled_spss"}. | ||
} |