Debian Codegraph @

R/tibbles.R

raw
1 setGeneric("prep_for_tibble",function(x,...)standardGeneric("prep_for_tibble"))
2 setMethod("prep_for_tibble",signature(x="item.vector"),function(x,...)
3 prep_for_tibble_item_vector(x))
4
5 prep_for_tibble_item_vector <- function(x,...){
6 y <- if(inherits(x,"character.item")) as.character(x)
7 else if(is.ordinal(x)) as.ordered(x)
8 else if(is.nominal(x)) as.factor(x)
9 else as.vector(x)
10 d <- description(x)
11 if(length(d))
12 attr(y,"label") <- d
13 return(y)
14 }
15
16 as_tibble.data.set <- function(x, ...){
17 y <- lapply([email protected],
18 prep_for_tibble_item_vector)
19 names(y) <- names(x)
20 attr(y,"row.names") <- x@row_names
21 class(y) <- c("tbl_df","tbl","data.frame")
22 return(y)
23 }
24
25 sanitize_labels <- function(labels){
26 labn <- names(labels)
27 if(length(unique(labels)) < length(labn)){
28 dup <- duplicated(labels)
29 warning(sprintf("Dropped non-unique label(s) %s",
30 paste(paste(labn[dup],"=",labels[dup]),collapse=", ")),
31 call.=FALSE,immediate.=TRUE)
32 labels <- labels[!dup]
33 }
34 labels
35 }
36
37 setOldClass("labelled")
38 setMethod("as.item",signature(x="labelled"),function(x,...){
39 annotation <- c(description=attr(x,"label",exact=TRUE))
40 labels <- attr(x,"labels",exact=TRUE)
41 labels <- sanitize_labels(labels)
42 attributes(x) <- NULL
43 as.item(x,labels=labels,
44 annotation=annotation)
45 })
46 setMethod("codebookEntry","labelled",function(x){
47 x <- as.item(x)
48 annotation <- annotation(x)
49 spec <- c(
50 "Storage mode:"=storage.mode(x),
51 "Measurement:"="undefined"
52 )
53 new("codebookEntry",
54 spec = spec,
55 stats = codebookStatsCateg(x),
56 annotation = annotation
57 )
58 })
59
60 # if(!requireNamespace("haven",quietly = TRUE))
61 setOldClass("haven_labelled")
62 setMethod("as.item",signature(x="haven_labelled"),function(x,...){
63 annotation <- c(description=attr(x,"label",exact=TRUE))
64 labels <- attr(x,"labels",exact=TRUE)
65 labels <- sanitize_labels(labels)
66 attributes(x) <- NULL
67 as.item(x,labels=labels,
68 annotation=annotation)
69 })
70 setMethod("codebookEntry","haven_labelled",function(x){
71 x <- as.item(x)
72 annotation <- annotation(x)
73 spec <- c(
74 "Storage mode:"=storage.mode(x),
75 "Measurement:"="undefined"
76 )
77 new("codebookEntry",
78 spec = spec,
79 stats = codebookStatsCateg(x),
80 annotation = annotation
81 )
82 })
83
84
85
86 # if(!requireNamespace("haven",quietly = TRUE))
87 setOldClass("haven_labelled_spss")
88 setMethod("as.item",signature(x="haven_labelled_spss"),function(x,...){
89 annotation <- c(description=attr(x,"label",exact=TRUE))
90 labels <- attr(x,"labels",exact=TRUE)
91 labels <- sanitize_labels(labels)
92 mis_range <- attr(x,"na_range")
93 mis_values <- attr(x,"na_values")
94 attributes(x) <- NULL
95 value_filter <- new("missing.values",
96 filter=mis_values,
97 range=mis_range)
98 as.item(x,labels=labels,
99 annotation=annotation,
100 value.filter=value_filter)
101 })
102 setMethod("codebookEntry","haven_labelled_spss",function(x){
103 x <- as.item(x)
104 annotation <- annotation(x)
105 filter <- [email protected]
106 spec <- c(
107 "Storage mode:"=storage.mode(x),
108 "Measurement:"="undefined"
109 )
110 if(length(filter)) spec <- c(spec,
111 switch(class(filter),
112 missing.values = c("Missing values:" = format(filter)),
113 valid.values = c("Valid values:" = format(filter)),
114 valid.range = c("Valid range:" = format(filter))
115 ))
116 new("codebookEntry",
117 spec = spec,
118 stats = codebookStatsCateg(x),
119 annotation = annotation
120 )
121 })
122
123 setGeneric("as_haven",function(x,...)standardGeneric("as_haven"))
124 setMethod("as_haven",signature(x="data.set"),function(x,user_na=FALSE,...){
125 y <- lapply([email protected],as_haven,user_na=user_na,...)
126 names(y) <- names(x)
127 names(y) <- gsub(".","_",names(y),fixed=TRUE)
128 attr(y,"row.names") <- x@row_names
129 class(y) <- c("tbl_df","tbl","data.frame")
130 return(y)
131 })
132 setMethod("as_haven",signature(x="item.vector"),function(x,user_na=FALSE,...){
133 y <- [email protected]
134 if(length(description(x)))
135 attr(y,"label") <- description(x)
136 if(is.character(x))
137 labels(x) <- NULL
138 if(length(labels(x))){
139 l <- as.vector(labels(x))
140 storage.mode(y) <- "integer"
141 storage.mode(l) <- "integer"
142 attr(y,"labels") <- l
143 ms <- missing.values(x)
144 if(user_na && length(ms)){
145 attr(y,"na_values") <- as.integer(ms@filter)
146 attr(y,"na_range") <- as.integer(ms@range)
147 class(y) <- "haven_labelled_spss"
148 } else {
149 ism <- is.missing(x)
150 y[ism] <- NA
151 class(y) <- "haven_labelled"
152 }
153 }
154 return(y)
155 })