根据首次发病对数据进行排名
Ranking data based on first onset
subset <-
structure(list(MEMORY1 = c(3L, 2L, 3L, 2L), MEMORY2 = c(3L, 2L,
3L, 1L), MEMORY3 = c(2L, 2L, 3L, 2L), MEMORY4 = c(2L, 2L, 2L,
2L), MEMORY5 = c(2L, 2L, 2L, 2L), MEMORY6 = c(1L, 1L, 1L, 1L),
MEMORY7 = c(2L, 2L, 2L, 2L), MEMORY8 = c(1L, 1L, 1L, 1L)), .Names = c("MEMORY1",
"MEMORY2", "MEMORY3", "MEMORY4", "MEMORY5", "MEMORY6", "MEMORY7",
"MEMORY8"), row.names = c(NA, -4L), class = "data.frame")
subset
# MEMORY1 MEMORY2 MEMORY3 MEMORY4 MEMORY5 MEMORY6 MEMORY7 MEMORY8
# 1 3 3 2 2 2 1 2 1
# 2 2 2 2 2 2 1 2 1
# 3 3 3 3 2 2 1 2 1
# 4 2 1 2 2 2 1 2 1
我有一个数据集,包含 4 个时间点(4 行)的 8 个内存项。我正在尝试根据首次出现对记忆项进行排名,首次出现定义为记忆项的值第一次 > 1。
对于上述 subset
,项目 1、2、3、4、5 和 7 的等级为 1,因为在时间 1
,这些项目的值 > 1。作为对于第 6 项和第 8 项,它们在所有 4 个时间点的值 = 1,因此我会给它们分配 NA
的排名。
ranks = rep(0, items)
ranks = sapply(subset, function(x) which(x > 1)[1L])
ranks
# MEMORY1 MEMORY2 MEMORY3 MEMORY4 MEMORY5 MEMORY6 MEMORY7 MEMORY8
# 1 1 1 1 1 NA 1 NA
但是,由于 MEMORY1
和 MEMORY2
在时间 1
和 MEMORY3
、MEMORY4
、MEMORY5
和MEMORY7
在时间 1
的值为 2,我想将 MEMORY1
和 MEMORY2
排在其他四个项目之前。所以我想要一个看起来像
的输出
ranks
# MEMORY1 MEMORY2 MEMORY3 MEMORY4 MEMORY5 MEMORY6 MEMORY7 MEMORY8
# 1 1 2 2 2 NA 2 NA
因此 1) 按第一次出现排名 2) 将最高值的项目排名为 1,下一个最高值为 2,依此类推。
我怎样才能完成步骤 2)?
这为您提供了以下值:
wheremax <- sapply(subset,function(x) { which(x > 1)[1L] }) #your code
as.matrix(subset)[cbind(wheremax,1:ncol(subset))]
[1] 3 3 2 2 2 NA 2 NA
然后您可以获得排名:
DTrank <- rank(-as.matrix(subset)[cbind(wheremax,1:ncol(subset))] + wheremax * max(subset), ties.method = "min", na.last = "keep")
[1] 1 1 3 3 3 NA 3 NA
这会为每个额外的行增加惩罚,等于 data.frame 中任意位置的最大值。它确保第二行中的值始终排在第一行中的值下方
但它不是按增量排序的(即 1、2、3、...)。不过,较高的数字将始终具有较低的值。如果有更好的方法,欢迎提出建议。
首先,获取排名和值:
df <- sapply(subset, function(x) {
tmp <- which(x > 1)[1L];
c(rank=tmp, val=ifelse(length(tmp>0), x[tmp], NA))
})
# adding "memory" field to keep track of the memories
df <- data.frame(t(df), memory=1:nrow(df))
# let's add a little excitement otherwise hard to tell if it's working
df[3,1] <- 2
# dealing with NA by giving them infinite rank
df[is.na(df)] <- Inf
# val will be sorted by increasing values, so take the neg because we want them decreasing
df$val <- -df$val
final_rank_order <- order(df$rank, df$val, decreasing = F)
df <- df[final_rank_order,]
df$final <- 1
for(i in 2:nrow(df)) {
if(df$rank[i]==df$rank[i-1] & df$val[i]==df$val[i-1])
df$final[i] <- df$final[i-1]
else
df$final[i] <- df$final[i-1]+1
}
此时我们有这个:
> df
rank val memory final
MEMORY1 1 -3 1 1
MEMORY2 1 -3 2 1
MEMORY4 1 -2 4 2
MEMORY5 1 -2 5 2
MEMORY7 1 -2 7 2
MEMORY3 2 -2 3 3
MEMORY6 Inf -Inf 6 4
MEMORY8 Inf -Inf 8 4
最后的接触:
final_ranks <- df$final[order(df$memory)]
> final_ranks
[1] 1 1 3 2 2 4 2 4
我简直不敢相信它会如此骇人听闻。一开始真的以为是小事。一定有更好的方法!
请注意,我稍微更改了您的数据,因为您只有 1 和 NA,因此无法判断代码是否有效。
subset <-
structure(list(MEMORY1 = c(3L, 2L, 3L, 2L), MEMORY2 = c(3L, 2L,
3L, 1L), MEMORY3 = c(2L, 2L, 3L, 2L), MEMORY4 = c(2L, 2L, 2L,
2L), MEMORY5 = c(2L, 2L, 2L, 2L), MEMORY6 = c(1L, 1L, 1L, 1L),
MEMORY7 = c(2L, 2L, 2L, 2L), MEMORY8 = c(1L, 1L, 1L, 1L)), .Names = c("MEMORY1",
"MEMORY2", "MEMORY3", "MEMORY4", "MEMORY5", "MEMORY6", "MEMORY7",
"MEMORY8"), row.names = c(NA, -4L), class = "data.frame")
subset
# MEMORY1 MEMORY2 MEMORY3 MEMORY4 MEMORY5 MEMORY6 MEMORY7 MEMORY8
# 1 3 3 2 2 2 1 2 1
# 2 2 2 2 2 2 1 2 1
# 3 3 3 3 2 2 1 2 1
# 4 2 1 2 2 2 1 2 1
我有一个数据集,包含 4 个时间点(4 行)的 8 个内存项。我正在尝试根据首次出现对记忆项进行排名,首次出现定义为记忆项的值第一次 > 1。
对于上述 subset
,项目 1、2、3、4、5 和 7 的等级为 1,因为在时间 1
,这些项目的值 > 1。作为对于第 6 项和第 8 项,它们在所有 4 个时间点的值 = 1,因此我会给它们分配 NA
的排名。
ranks = rep(0, items)
ranks = sapply(subset, function(x) which(x > 1)[1L])
ranks
# MEMORY1 MEMORY2 MEMORY3 MEMORY4 MEMORY5 MEMORY6 MEMORY7 MEMORY8
# 1 1 1 1 1 NA 1 NA
但是,由于 MEMORY1
和 MEMORY2
在时间 1
和 MEMORY3
、MEMORY4
、MEMORY5
和MEMORY7
在时间 1
的值为 2,我想将 MEMORY1
和 MEMORY2
排在其他四个项目之前。所以我想要一个看起来像
ranks
# MEMORY1 MEMORY2 MEMORY3 MEMORY4 MEMORY5 MEMORY6 MEMORY7 MEMORY8
# 1 1 2 2 2 NA 2 NA
因此 1) 按第一次出现排名 2) 将最高值的项目排名为 1,下一个最高值为 2,依此类推。
我怎样才能完成步骤 2)?
这为您提供了以下值:
wheremax <- sapply(subset,function(x) { which(x > 1)[1L] }) #your code
as.matrix(subset)[cbind(wheremax,1:ncol(subset))]
[1] 3 3 2 2 2 NA 2 NA
然后您可以获得排名:
DTrank <- rank(-as.matrix(subset)[cbind(wheremax,1:ncol(subset))] + wheremax * max(subset), ties.method = "min", na.last = "keep")
[1] 1 1 3 3 3 NA 3 NA
这会为每个额外的行增加惩罚,等于 data.frame 中任意位置的最大值。它确保第二行中的值始终排在第一行中的值下方
但它不是按增量排序的(即 1、2、3、...)。不过,较高的数字将始终具有较低的值。如果有更好的方法,欢迎提出建议。
首先,获取排名和值:
df <- sapply(subset, function(x) {
tmp <- which(x > 1)[1L];
c(rank=tmp, val=ifelse(length(tmp>0), x[tmp], NA))
})
# adding "memory" field to keep track of the memories
df <- data.frame(t(df), memory=1:nrow(df))
# let's add a little excitement otherwise hard to tell if it's working
df[3,1] <- 2
# dealing with NA by giving them infinite rank
df[is.na(df)] <- Inf
# val will be sorted by increasing values, so take the neg because we want them decreasing
df$val <- -df$val
final_rank_order <- order(df$rank, df$val, decreasing = F)
df <- df[final_rank_order,]
df$final <- 1
for(i in 2:nrow(df)) {
if(df$rank[i]==df$rank[i-1] & df$val[i]==df$val[i-1])
df$final[i] <- df$final[i-1]
else
df$final[i] <- df$final[i-1]+1
}
此时我们有这个:
> df
rank val memory final
MEMORY1 1 -3 1 1
MEMORY2 1 -3 2 1
MEMORY4 1 -2 4 2
MEMORY5 1 -2 5 2
MEMORY7 1 -2 7 2
MEMORY3 2 -2 3 3
MEMORY6 Inf -Inf 6 4
MEMORY8 Inf -Inf 8 4
最后的接触:
final_ranks <- df$final[order(df$memory)]
> final_ranks
[1] 1 1 3 2 2 4 2 4
我简直不敢相信它会如此骇人听闻。一开始真的以为是小事。一定有更好的方法!
请注意,我稍微更改了您的数据,因为您只有 1 和 NA,因此无法判断代码是否有效。