我的代码有一个新问题,与https://stackoverflow.com/posts/56680932/edit
中的第一个版本有关我意识到我的输出是错误的,因为我一直想将“优先级”值较小的“类别”值保存在“新”列中。
以下是更新的代码:
priority <- c(3,2,1,4,5,6,7)
category <- c("a","b","c","d","e","f","g")
library(data.table)
data.dt <- data.table(priority,category)
data.dt$new <- NA
data.dt$new2 <- NA
data.dt$rest <- NA
for (i in 2:nrow(data.dt)){
if(data.dt$priority[i]<=ifelse(is.na(data.dt$new[i-1]),data.dt$priority[i-1],data.dt$new[i-1])){
data.dt$new[[i]] <- data.dt$priority[i]
data.dt$new2[[i]] <- data.dt$category[i]
data.dt$rest[[i]]<- toString(data.dt$category[1:(i-1)])
}
else{
data.dt$new[[i]] <- data.dt$new[i-1]
data.dt$new2[[i]] <- data.dt$new2[i-1]
data.dt$rest[[i]] <- toString(data.dt$category[1:i])
}
}
及其输出:
> data.dt
priority category new new2 rest
1: 3 a NA <NA> <NA>
2: 2 b 2 b a
3: 1 c 1 c a, b
4: 4 d 1 c a, b, c, d
5: 5 e 1 c a, b, c, d, e
6: 6 f 1 c a, b, c, d, e, f
7: 7 g 1 c a, b, c, d, e, f, g
如果我添加@ jay.sf的代码以将“ new2”的值从“ rest”删除:
priority <- c(3,2,1,4,5,6,7)
category <- c("a","b","c","d","e","f","g")
library(data.table)
data.dt <- data.table(priority,category)
data.dt$new <- NA
data.dt$new2 <- NA
data.dt$rest <- NA
for (i in 2:nrow(data.dt)){
if(data.dt$priority[i]<=ifelse(is.na(data.dt$new[i-1]),data.dt$priority[i-1],data.dt$new[i-1])){
data.dt$new[[i]] <- data.dt$priority[i]
data.dt$new2[[i]] <- data.dt$category[i]
#data.dt$rest[[i]]<- toString(data.dt$category[1:(i-1)])
}
else{
data.dt$new[[i]] <- data.dt$new[i-1]
data.dt$new2[[i]] <- data.dt$new2[i-1]
#data.dt$rest[[i]] <- toString(data.dt$category[1:i])
}
}
sc <- Map(function(x) c(data.dt$category[seq(x)]), seq(nrow(data.dt)))
data.dt$rest <- unlist(c(NA, Map(function(x, y)
toString(x[is.na(match(x, y))]), sc, data.dt$new2)[-1]))
输出为:
> data.dt
priority category new new2 rest
1: 3 a NA <NA> <NA>
2: 2 b 2 b a
3: 1 c 1 c a, b
4: 4 d 1 c a, b, d
5: 5 e 1 c a, b, d, e
6: 6 f 1 c a, b, d, e, f
7: 7 g 1 c a, b, d, e, f, g
我知道这不是最佳方法,但是正在起作用。您能告诉我是否有更合适的方法吗?
另一方面,我想根据要添加到数据表中的另一列进行比较。这是示例:
> data.dt
overlap priority category new new2 rest
1: 0 3 a NA <NA> <NA>
2: 1 2 b 2 b a
3: 1 1 c 1 c a, b
4: 1 4 d 1 c a, b, d
5: 0 5 e NA NA NA
6: 1 6 f 1 e f
7: NA 7 g 1 e f,g