答案 0 :(得分:3)
这样的事情可以解决问题: -
##' @importFrom reshape2 melt
##' @importFrom plyr ldply name_rows
augment.ranef.mer <- function(x,
ci.level=0.9,
reorder=TRUE,
order.var=1) {
tmpf <- function(z) {
if (is.character(order.var) && !order.var %in% names(z)) {
order.var <- 1
warning("order.var not found, resetting to 1")
}
## would use plyr::name_rows, but want levels first
zz <- data.frame(level=rownames(z),z,check.names=FALSE)
if (reorder) {
## if numeric order var, add 1 to account for level column
ov <- if (is.numeric(order.var)) order.var+1 else order.var
zz$level <- reorder(zz$level, zz[,order.var+1], FUN=identity)
}
## Q-Q values, for each column separately
qq <- c(apply(z,2,function(y) {
qnorm(ppoints(nrow(z)))[order(order(y))]
}))
rownames(zz) <- NULL
pv <- attr(z, "postVar")
cols <- 1:(dim(pv)[1])
se <- unlist(lapply(cols, function(i) sqrt(pv[i, i, ])))
## n.b.: depends on explicit column-major ordering of se/melt
zzz <- cbind(melt(zz,id.vars="level",value.name="estimate"),
qq=qq,std.error=se)
## reorder columns:
subset(zzz,select=c(variable, level, estimate, qq, std.error))
}
dd <- ldply(x,tmpf,.id="grp")
ci.val <- -qnorm((1-ci.level)/2)
transform(dd,
p=2*pnorm(-abs(estimate/std.error)), ## 2-tailed p-val
lb=estimate-ci.val*std.error,
ub=estimate+ci.val*std.error)
}
答案 1 :(得分:0)
如果您的第一个数据集由查询
表示SELECT
RegionalManager,
StoreName,
StoreRank
FROM
#Stores
然后可以使用此查询完成第二个结果
SELECT
RegionalManager,
StoreName,
StoreRank,
ROW_NUMBER() OVER (PARTITION BY RegionalManager ORDER BY StoreRank ASC) AS 'GroupRank'
FROM
#Stores
Mat Richardson似乎使用函数RANK()而不是ROW_NUMBER()给出了相同的答案。 RANK()会给出关系,即2个组可以具有相同的等级,而ROW_NUMBER()则不会。
答案 2 :(得分:0)
我认为只有Row_number
或Rank
或Dense_Rank
才足以获得结果。
SELECT
RegionalManager,
StoreName,
StoreRank,
((ROW_NUMBER() OVER (ORDER BY StoreRank ASC) - 1 ) / 3 ) + 1 AS GroupRank
FROM
Yourtable
order by RegionalManager,StoreName.
如评论中所述,不确定为什么您的存储库在样本数据和预期结果之间发生了变化