| 1 |
|
| 2 |
unlist1 <- function(x)unlist(as.list(x[-1])) |
| 3 |
|
| 4 |
get_vnames_or_eval <- function(x,envir=NULL,enclos=parent.frame()){ |
| 5 |
if(inherits(x,"call") && deparse(x[[1]]) == "c"){ |
| 6 |
return(sapply(x[-1],as.character)) |
| 7 |
} |
| 8 |
else { |
| 9 |
res <- try(eval(x,envir=envir,enclos=enclos),silent=TRUE) |
| 10 |
if(inherits(res,"try-error")) |
| 11 |
return(NULL) |
| 12 |
else return(res) |
| 13 |
} |
| 14 |
} |
| 15 |
|
| 16 |
get_vnames <- function(x,envir=NULL,enclos=parent.frame()){ |
| 17 |
if(length(x) == 1) return(as.character(x)) |
| 18 |
else if(inherits(x,"call") && deparse(x[[1]]) == "c"){ |
| 19 |
return(sapply(x[-1],as.character)) |
| 20 |
} |
| 21 |
else { |
| 22 |
return(NULL) |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
all_in_names <- function(x,names){ |
| 28 |
all(!nzchar(x) | x %in% names) |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
Reshape <- function(data,...,id,within_id,drop,keep,direction){ |
| 33 |
|
| 34 |
if(inherits(data,"data.set")){ |
| 35 |
cdpl <- codeplan(data) |
| 36 |
reshape_attr <- attributes(data)[c("reshapeLong","reshapeWide")] |
| 37 |
codeplan(data) <- NULL |
| 38 |
attributes(data)[c("reshapeLong","reshapeWide")] <- reshape_attr |
| 39 |
} |
| 40 |
else cdpl <- NULL |
| 41 |
|
| 42 |
mycall <- match.call(expand.dots=FALSE) |
| 43 |
|
| 44 |
spec <- mycall$... |
| 45 |
m <- length(spec) |
| 46 |
|
| 47 |
if(m > 1 && missing(within_id)) |
| 48 |
vspec <- spec[-m] |
| 49 |
else |
| 50 |
vspec <- spec |
| 51 |
if(length(vspec)==1 && |
| 52 |
deparse(vspec[[1]][[1]]) == "list"){ |
| 53 |
vspec <- as.list(vspec[[1]][-1]) |
| 54 |
} |
| 55 |
vspec <- lapply(vspec,get_vnames_or_eval,envir=data,enclos=parent.frame()) |
| 56 |
v.names <- names(vspec) |
| 57 |
varying <- vspec |
| 58 |
varying <- lapply(varying,function(x){ |
| 59 |
x[!nzchar(x)] <- "__tmp__na__" |
| 60 |
return(x) |
| 61 |
}) |
| 62 |
|
| 63 |
if(!missing(drop)) |
| 64 |
drop <- intersect(get_vnames(mycall$drop,envir=data), |
| 65 |
names(data)) |
| 66 |
else |
| 67 |
drop <- NULL |
| 68 |
if(!missing(within_id)){ |
| 69 |
within_id <- intersect(get_vnames(mycall$within_id,envir=data), |
| 70 |
names(data)) |
| 71 |
timevar <- within_id |
| 72 |
times <- unique(data[[timevar]]) |
| 73 |
} |
| 74 |
else if(m > 1){ |
| 75 |
times <- eval(spec[[m]],envir=data,enclos=parent.frame()) |
| 76 |
timevar <- names(spec)[m] |
| 77 |
} |
| 78 |
else { |
| 79 |
n <- length(varying[[1]]) |
| 80 |
times <- 1:n |
| 81 |
timevar <- "times" |
| 82 |
} |
| 83 |
|
| 84 |
if(direction=="long"){ |
| 85 |
data[["__tmp__na__"]] <- NA |
| 86 |
cls <- class(data) |
| 87 |
class(data) <- "data.frame" |
| 88 |
res <- reshape(data, |
| 89 |
varying=varying, |
| 90 |
v.names=v.names, |
| 91 |
times=times, |
| 92 |
timevar=timevar, |
| 93 |
drop=drop, |
| 94 |
direction=direction) |
| 95 |
res["__tmp__na__"] <- NULL |
| 96 |
if(is.character(times)) |
| 97 |
res[[timevar]] <- factor(res[[timevar]], |
| 98 |
levels=times) |
| 99 |
idvar <- "id" |
| 100 |
ii <- order(res[[idvar]],res[[timevar]]) |
| 101 |
res <- res[ii,] |
| 102 |
if(!missing(keep)){ |
| 103 |
keep <- intersect(get_vnames(mycall$keep,envir=data),names(data)) |
| 104 |
keep <- union(keep,c(v.names,timevar)) |
| 105 |
keep <- intersect(keep,names(res)) |
| 106 |
res <- res[keep] |
| 107 |
} |
| 108 |
class(res) <- cls |
| 109 |
|
| 110 |
} else { |
| 111 |
rlattr <- attr(data,"reshapeLong") |
| 112 |
drop_id <- FALSE |
| 113 |
if(missing(id)){ |
| 114 |
if(length(rlattr) && length(rlattr$idvar)) |
| 115 |
idvar <- rlattr$idvar |
| 116 |
else { |
| 117 |
if("id" %in% names(data)) idvar <- "id" |
| 118 |
else stop("missing id variable, please provide one with idvar=...") |
| 119 |
} |
| 120 |
idvars <- idvar |
| 121 |
} |
| 122 |
else { |
| 123 |
idvars <- mycall$id |
| 124 |
idvars <- get_vnames(idvars,envir=data) |
| 125 |
if(length(idvars)>1){ |
| 126 |
ids <- data[,idvars,drop=FALSE] |
| 127 |
data[["id"]] <- as.integer(do.call(interaction,ids)) |
| 128 |
idvar <- "id" |
| 129 |
drop_id <- TRUE |
| 130 |
} |
| 131 |
else idvar <- idvars |
| 132 |
} |
| 133 |
if(timevar %nin% names(data)){ |
| 134 |
stop("missing within_id variable, please provide one.") |
| 135 |
} |
| 136 |
cls <- class(data) |
| 137 |
class(data) <- "data.frame" |
| 138 |
res <- reshape(as.data.frame(data), |
| 139 |
varying=varying, |
| 140 |
v.names=v.names, |
| 141 |
times=times, |
| 142 |
timevar=timevar, |
| 143 |
idvar=idvar, |
| 144 |
drop=drop, |
| 145 |
direction=direction) |
| 146 |
varying_ <- unname(unlist(varying)) |
| 147 |
nonvar_ <- setdiff(names(res),varying_) |
| 148 |
ii <- c(nonvar_,varying_) |
| 149 |
attr_reshape <- attr(res,"reshapeWide") |
| 150 |
res <- res[ii] |
| 151 |
if(!missing(keep)){ |
| 152 |
keep <- intersect(get_vnames(mycall$keep,envir=data),names(data)) |
| 153 |
keep <- union(keep,c(unlist(varying),times)) |
| 154 |
keep <- intersect(keep,names(res)) |
| 155 |
res <- res[keep] |
| 156 |
} |
| 157 |
if("__tmp__na__" %in% names(res)) |
| 158 |
res[["__tmp__na__"]] <- NULL |
| 159 |
else |
| 160 |
attr(res,"reshapeWide") <- attr_reshape |
| 161 |
if(drop_id) |
| 162 |
res[[idvar]] <- NULL |
| 163 |
class(res) <- cls |
| 164 |
} |
| 165 |
|
| 166 |
if(length(cdpl)){ |
| 167 |
reshape_attr <- attributes(res)[c("reshapeLong","reshapeWide")] |
| 168 |
codeplan(res) <- cdpl |
| 169 |
attributes(res)[c("reshapeLong","reshapeWide")] <- reshape_attr |
| 170 |
} |
| 171 |
|
| 172 |
res |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
# if(direction=="wide"){ |
| 178 |
# reshapeAttr <- attr(res,"reshapeLong") |
| 179 |
# varying <- unlist(varying) |
| 180 |
# othervars <- !(names(res) %in% varying) |
| 181 |
# othervars <- names(res)[othervars] |
| 182 |
# varying <- varying[varying %in% names(res)] |
| 183 |
# res <- res[c(othervars,varying)] |
| 184 |
# rownames(res) <- NULL |
| 185 |
# } |