我有一个逻辑向量,我希望在特定索引处插入新元素.我在下面提出了一个笨拙的解决方案,但有更简洁的方法吗?
probes <- rep(TRUE, 15)
ind <- c(5, 10)
probes.2 <- logical(length(probes)+length(ind))
probes.ind <- ind + 1:length(ind)
probes.original <- (1:length(probes.2))[-probes.ind]
probes.2[probes.ind] <- FALSE
probes.2[probes.original] <- probes
print(probes)
Run Code Online (Sandbox Code Playgroud)
给
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
Run Code Online (Sandbox Code Playgroud)
和
print(probes.2)
Run Code Online (Sandbox Code Playgroud)
给
[1] TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE FALSE
[13] TRUE TRUE TRUE TRUE TRUE
Run Code Online (Sandbox Code Playgroud)
所以它有效,但看起来很难看 - 有什么建议吗?
我有这个结构的以下数据表:
+-------------------+
| id | key | value |
+-----+-----+-------+
| 1 | A | 1000 |
| 1 | A | 2000 |
| 1 | B | 2001 |
| 1 | A | 2002 |
| 1 | A | 2004 |
| 2 | B | 2002 |
| 2 | C | 2002 |
+-------------------+
Run Code Online (Sandbox Code Playgroud)
我的目标是通过id和key对值进行求和,而不是仅仅通过id和key进行分组,我只想在连续行的id和key对相同的情况下对值进行求和.
结果应该是:
+-------------------+
| id | key | value |
+-----+-----+-------+
| 1 | A | 3000 |
| 1 …Run Code Online (Sandbox Code Playgroud)