考虑一个长度为n的列表
a <- list(c(5, 41, 75, 158, 269, 432, 630, 901, 1534))
我正在尝试在此列表中进行连续减法。我的目标是按顺序执行1的减法运算,使列表变长,使每个元素比以前的元素少1。
因此,我希望列表中包含以下数字:
a <- list(c(5, 41-1, 75-2, 158-3, 269-4, 432-5, 630-6, 901-7, 1534-8))
我的“伪代码”如下(我在现实生活中无法做到)
a[[1]][n] <- (a[[1]][n]-(n-1))
但是这再次在真实R中不起作用。
对此有何建议?
谢谢!
答案 0 :(得分:2)
因为它是1个向量的list
,所以用vector
提取[[
并从'a'的序列中减去(减去1)
a[[1]] <- a[[1]] - (seq_along(a[[1]]) - 1)
a
#[[1]]
# [1] 5 40 73 155 265 427 624 894 1526
如果它是一个list
且有多个vector
,请使用lapply
lapply(a, function(x) x - (seq_along(x) - 1))
或与relist
relist(unlist(a) - (sequence(lengths(a))-1), skeleton = a)
答案 1 :(得分:1)
return <svg
onClick={e => this.props.currentMounting.onMountingSelected(e.currentTarget.id)}
opacity={this.props.currentMounting.opacity}
style={{ fill: 'red' }}
id={this.props.currentMounting.id}>
<rect onMouseOver={e => this.props.currentMounting.onMountingHoveredOver(e)}
onMouseOut={e => this.props.currentMounting.onMountingHoveredOut(e)} x="0" y="0" height="60" width="60" />
<rect onMouseOver={e => this.props.currentMounting.onMountingHoveredOver(e)}
onMouseOut={e => this.props.currentMounting.onMountingHoveredOut(e)} x="70" y="0" height="60" width="60" />
</svg>