我有这样两列数据:
v1 = c(0, 29, 32, 29, 32, 28, -59, 30)
v2 = c(0, 0, 0, 0, 1, 1, 0, 1)
data = data.frame(v1, v2)
v1 v2
1 0 0
2 29 0
3 32 0
4 29 0
5 32 1
6 28 1
7 -59 0
8 30 1
我想按如下方式更改v2列的值:如果v1的值为负,那么将v2之前的所有1s更改为0.我可以使用R
应用for循环来实现这一点.有没有一种方法可以在不应用for循环的情况下做同样的事情(可能使用dplyr
包)?
Update My actual problem is more general than the situation I explained earlier. I apologize for the confusion. My data can have more than one negative values like in the given example: