r基于另一列值增加列值

时间:2016-03-11 19:28:05

标签: r

我有像这样的数据表x

+----+---------------+-------+
| id |      arg      | value |
+----+---------------+-------+
|  1 | New Day       | NA    |
|  2 | Eat breakfast | 3     |
|  3 | Bike          | 45    |
|  4 | New Day       | 0     |
|  5 | Get coffee    | 1     |
|  6 | Exercise      | 15    |
|  7 | Get beer      | NA    |
|  8 | New Day       |       |
|  9 | Pet cat       |       |
+----+---------------+-------+

我想为每天添加一个递增列来获得类似这样的内容

+----+---------------+-------+-----+
| id |      arg      | value | day |
+----+---------------+-------+-----+
|  1 | New Day       | NA    |   1 |
|  2 | Eat breakfast | 3     |   1 |
|  3 | Bike          | 45    |   1 |
|  4 | New Day       | 0     |   2 |
|  5 | Get coffee    | 1     |   2 |
|  6 | Exercise      | 15    |   2 |
|  7 | Get beer      | NA    |   2 |
|  8 | New Day       |       |   3 |
|  9 | Pet cat       |       |   3 |
+----+---------------+-------+-----+

我试过这个没有太大成功

x$day <-0
x<-within(x, day<-ifelse(arg == "New day", day+1, day))

0 个答案:

没有答案