我知道这是基本的rx东西,但是当事情决定解雇时,我有点困惑。请采取以下措施:
library('data.table')
DT <- data.table(ID=c(1,2,3,4,8,6,12,8,9),
position=c('A3','A1','B2','A2','B1','B3','B2','A1','B3'),
value=c(15,22,92,17,55,37,16,35,13),
A1=NA, A2=NA, A3=NA, B1=NA, B2=NA, B3=NA)
# Convert logical NAs to numeric NAs
DT[, 4:9] <- DT[, lapply(.SD, as.numeric), .SDcols=4:9]
# Generate "slot" vector using matching
slot <- match(DT$position, colnames(DT)[4:9])
# Loop thru each row of DT
for(i in 1:nrow(DT)){
DT[i, 3+slot[i]] <- DT[i,]$value
}
print(DT)
# ID position value A1 A2 A3 B1 B2 B3
# 1: 1 A3 15 NA NA 15 NA NA NA
# 2: 2 A1 22 22 NA NA NA NA NA
# 3: 3 B2 92 NA NA NA NA 92 NA
# 4: 4 A2 17 NA 17 NA NA NA NA
# 5: 8 B1 55 NA NA NA 55 NA NA
# 6: 6 B3 37 NA NA NA NA NA 37
# 7: 12 B2 16 NA NA NA NA 16 NA
# 8: 8 A1 35 35 NA NA NA NA NA
# 9: 9 B3 13 NA NA NA NA NA 13
为什么在订阅之前没有做任何事情?在我将订阅调用添加到链的末尾之前,不会触发任何日志:
Observable.of([1,2,3,4,5])
.combineLatest([6,7,8,9,10])
.take(1)
.do(([first, second]) => console.log(first, second))
// ...Logs nothing...
另外,如果我理解正确,我不需要取消订阅,因为take(1)会为我处理,对吗?
答案 0 :(得分:0)
您的示例生成 cold observable - 在您订阅之前不会生成的 cold 。替代方案是 hot observable,它不管观察者如何生成并在观察者之间共享。见hot and cold observables
您可以通过提供onCompleted
回调(订阅中的第三个参数)来检查序列是否终止。像这样:
rx.Observable.from([1,2,3,4,5])
.combineLatest(rx.Observable.from([6,7,8,9,10]))
.take(1)
.do(([first, second]) => console.log(first, second))
.subscribe(() => console.log('Subscribed'), err=> { console.error(err)}, () => console.log('completed!!'));