我有一个这样的数据框:
a=c("A","A","A","A","B","B","C","C","C","D","D")
b=c(1,2,3,4,1,2,1,2,3,1,2)
c=c(1345,645,75,8,95,678,598,95,75,4,53)
mydf <- data.frame(a,b,c) # edit note: do _not_ use cbind inside data.frame
我的目标是在新数据框上添加一个额外的列,该列将采用列“c”的最后一个值,并考虑“a”列中的因子。 更具体地说,在这个考试中,最终结果是这样的:
a b c d
1 A 1 1345 0
2 A 2 645 0
3 A 3 75 0
4 A 4 8 8
5 B 1 95 0
6 B 2 678 678
7 C 1 598 0
8 C 2 95 0
9 C 3 75 75
10 D 1 4 0
11 D 2 53 53
答案 0 :(得分:2)
如果您不需要将变量作为所有因素,那么let promise1 = new Promise(resolve => {
setTimeout(resolve, 500, 'one');
});
let promise2 = new Promise(resolve => {
setTimeout(resolve, 800, 'two');
});
Promise.race([promise1, promise2]).then(result => console.log(result));
就是一个不错的解决方案:
dplyr
答案 1 :(得分:0)
使用data.table
:
library(data.table)
df <- data.frame(a,b,c)
setDT(df)
df[, idx := .N, by = a]
df[, id := 1:.N, by = a]
df <- df[id == idx, d := c]
df[, c("id", "idx") := NULL]
df[is.na(df)] <- 0
a b c d
1: A 1 1345 0
2: A 2 645 0
3: A 3 75 0
4: A 4 8 8
5: B 1 95 0
6: B 2 678 678
7: C 1 598 0
8: C 2 95 0
9: C 3 75 75
10: D 1 4 0
11: D 2 53 53