我有一个数据集"运动员"比赛"比赛" ("匹配" == 1)随机"日期"。例如:
df <- data.frame(matrix(nrow = 80, ncol = 5))
colnames(df) <- c("Athlete", "Date", "Match", "DaysAfter", "DaysBefore")
df[,"Athlete"] <- c(rep(1, 20), rep(2,20), rep(3, 20), rep(4, 20))
df[,"Date"] <- rep(1:20, 4)
df[,"Match"] <- c(0,0,0,0,1,0,0,1,0,0)
我想制作两个变量:
df$DaysAfter <- # number of days after last "Match" (for each "Athlete").
df$DaysBefore <- # number of days before next "Match" (for each "Athlete").
PS!当&#34;匹配&#34; == 1,然后&#34; DaysAfter&#34;和&#34; DaysBefore&#34;应该是0。 在&#34; DaysAfter&#34;之前没有比赛时在&#34; DaysBefore&#34;之后,显示NA(参见示例)。
我希望数据集看起来像这样:
Ath Dat Mat DA DB
1 1 0 NA -4
1 2 0 NA -3
1 3 0 NA -2
1 4 0 NA -1
1 5 1 0 0
1 6 0 1 -2
1 7 0 2 -1
1 8 1 0 0
1 9 0 1 -4
1 10 0 2 -3
1 11 0 3 -2
1 12 0 4 -1
1 13 1 0 0
1 14 0 1 -2
1 15 0 2 -1
1 16 1 0 0
1 17 0 1 NA
1 18 0 2 NA
1 19 0 3 NA
1 20 0 4 NA
2 1 0 NA -4
2 2 0 NA -3
etc.
我怎样才能做到这一点?
答案 0 :(得分:2)
我们可以使用data.table
。转换&#39; data.frame&#39;到&#39; data.table&#39; (setDT(df)
),由运动员&#39;分组。另一个分组变量是根据匹配&#39;中的1的位置创建的。 (cumsum(Match == 1)
),我们创建了两列 -
1)DA - 我们需要NA
所有元素,直到匹配&#39;中的第一个,然后使用if/else
创建逻辑条件,以便all
匹配&#39;中的0为0的元素将乘以&#39; NA&#39; (NA *任何数字都返回NA)。当我们按cumsum
进行分组时,只有第一组将所有元素都设为0,因此该部分得到了解决。 else
条件获取行序列并从中减去1(`.seq_len(.N)-1)。
2)DB - 我们将匹配&#39;使用行数(.N
)并从反向序列(.N:1
)中减去。完成此操作后,最后一部分涉及在匹配&#39;中的最后一个之后为列中的元素创建NA。由运动员&#39;分组,我们得到序列中的行索引(.I
)来自匹配&#39;匹配&#39; (下一个元素)到行数(.N
),并指定(:=
)&#39; DB&#39;基于该指数的NA。
library(data.table)
df1 <- setDT(df)[, c("DA", "DB") := list(if(all(!Match)) NA*Match else
seq_len(.N)-1,Match*(.N) -(.N:1)) , by = .(cumsum(Match==1), Athlete)]
df1[df1[, .I[(max(which(Match==1))+1):.N] , by = Athlete]$V1, DB:= NA][]
# Athlete Date Match DA DB
# 1: 1 1 0 NA -4
# 2: 1 2 0 NA -3
# 3: 1 3 0 NA -2
# 4: 1 4 0 NA -1
# 5: 1 5 1 0 0
# 6: 1 6 0 1 -2
# 7: 1 7 0 2 -1
# 8: 1 8 1 0 0
# 9: 1 9 0 1 -6
#10: 1 10 0 2 -5
#11: 1 11 0 3 -4
#12: 1 12 0 4 -3
#13: 1 13 0 5 -2
#14: 1 14 0 6 -1
#15: 1 15 1 0 0
#16: 1 16 0 1 -2
#17: 1 17 0 2 -1
#18: 1 18 1 0 0
#19: 1 19 0 1 NA
#20: 1 20 0 2 NA
#21: 2 1 0 NA -4
#22: 2 2 0 NA -3
#23: 2 3 0 NA -2
#24: 2 4 0 NA -1
#25: 2 5 1 0 0
#26: 2 6 0 1 -2
#27: 2 7 0 2 -1
#28: 2 8 1 0 0
#29: 2 9 0 1 -6
#30: 2 10 0 2 -5
#31: 2 11 0 3 -4
#32: 2 12 0 4 -3
#33: 2 13 0 5 -2
#34: 2 14 0 6 -1
#35: 2 15 1 0 0
#36: 2 16 0 1 -2
#37: 2 17 0 2 -1
#38: 2 18 1 0 0
#39: 2 19 0 1 NA
#40: 2 20 0 2 NA
#41: 3 1 0 NA -4
#42: 3 2 0 NA -3
#43: 3 3 0 NA -2
#44: 3 4 0 NA -1
#45: 3 5 1 0 0
#46: 3 6 0 1 -2
#47: 3 7 0 2 -1
#48: 3 8 1 0 0
#49: 3 9 0 1 -6
#50: 3 10 0 2 -5
#51: 3 11 0 3 -4
#52: 3 12 0 4 -3
#53: 3 13 0 5 -2
#54: 3 14 0 6 -1
#55: 3 15 1 0 0
#56: 3 16 0 1 -2
#57: 3 17 0 2 -1
#58: 3 18 1 0 0
#59: 3 19 0 1 NA
#60: 3 20 0 2 NA
#61: 4 1 0 NA -4
#62: 4 2 0 NA -3
#63: 4 3 0 NA -2
#64: 4 4 0 NA -1
#65: 4 5 1 0 0
#66: 4 6 0 1 -2
#67: 4 7 0 2 -1
#68: 4 8 1 0 0
#69: 4 9 0 1 -6
#70: 4 10 0 2 -5
#71: 4 11 0 3 -4
#72: 4 12 0 4 -3
#73: 4 13 0 5 -2
#74: 4 14 0 6 -1
#75: 4 15 1 0 0
#76: 4 16 0 1 -2
#77: 4 17 0 2 -1
#78: 4 18 1 0 0
#79: 4 19 0 1 NA
#80: 4 20 0 2 NA
答案 1 :(得分:1)
此代码应该有效:
WebElement el = webDriver.findElement(By.xpath("here is your xpath"));
JavascriptExecutor executor = (JavascriptExecutor)webDriver
executor.executeScript("arguments[0].click()", el);
答案 2 :(得分:0)
我曾写过以下函数:
cumsum.r <- function (vals, restart)
{
if (!is.vector(vals) || !is.vector(restart))
stop("expect vectors")
if (length(vals) != length(restart))
stop("different length")
len = length(vals)
restart[1] = T
ind = which(restart)
ind = rep(ind, c(ind[-1], len + 1) - ind)
vals.c = cumsum(vals)
vals.c - vals.c[ind] + vals[ind]
}
它执行cumsum,但只要restart = TRUE,就从零开始。
对于&#34;&#34;之后的日子,您需要
new.ath <- c(TRUE, df$Ath[-1]==df$Ath[-length(df$Ath)])
restart <- df$Math==1 | new.ath
days.after <- cumsum.r(1-restart, restart)
天数。在您需要之前
rr <- rev(restart)
days.before <- -rev(cumsum.r(1-rr, rr))
(这不会使用NAs,但您也可以将此cumsum.r用于NAs。)