将行值乘以R中的所有先前行值

时间:2018-05-21 21:15:10

标签: r

对于所有行,如何将行的值乘以该行的所有先前值?

df$new <- with(df, c(as[1],as[-1]*as[-nrow(df)]))

我可以使用以下方法将行值与前一个值相乘:

23:02:22.740 [Studio-akka.actor.default-dispatcher-12] ERROR 

com.datastax.studio.client.SchemaManager ID:  TS: - Unexpected exception getting cql schema for connection null: java.lang.NullPointerException
23:02:22.744 [Studio-akka.actor.default-dispatcher-42] WARN  com.datastax.studio.streaming.websocket.CQLSchemaBroadcaster ID:  TS: - Schema stream provider got null cql schema
23:02:22.740 [Studio-akka.actor.default-dispatcher-32] ERROR com.datastax.studio.application.EventuateEventBus ID:  TS: - Unhandled exception during publishing (event=SparkSQLConnectionClosed(connectionId=null), listener=com.datastax.studio.client.SchemaManager$$Lambda$71/1502197135@4121ef79):
java.lang.NullPointerException: null
    at java.util.concurrent.ConcurrentHashMap.replaceNode(ConcurrentHashMap.java:1106) ~[?:1.8.0_161]
    at java.util.concurrent.ConcurrentHashMap.remove(ConcurrentHashMap.java:1097) ~[?:1.8.0_161]
    at com.datastax.studio.client.SchemaManager.stopSparkSqlSchemaRefresh(SchemaManager.java:473) ~[classes/:?]
    at com.datastax.studio.client.SchemaManager.lambda$init$5(SchemaManager.java:181) ~[classes/:?]
    at com.datastax.studio.application.EventuateEventBus$EventListenerActor.lambda$new$0(EventuateEventBus.java:116) ~[classes/:?]
    at akka.japi.pf.UnitCaseStatement.apply(CaseStatements.scala:26) [akka-actor_2.11-2.4.17.jar:?]
    at akka.japi.pf.UnitCaseStatement.apply(CaseStatements.scala:21) [akka-actor_2.11-2.4.17.jar:?]
    at scala.PartialFunction$class.applyOrElse(PartialFunction.scala:123) [scala-library-2.11.8.jar:?]
    at akka.japi.pf.UnitCaseStatement.applyOrElse(CaseStatements.scala:21) [akka-actor_2.11-2.4.17.jar:?]
    at scala.PartialFunction$OrElse.apply(PartialFunction.scala:167) [scala-library-2.11.8.jar:?]
    at com.rbmhtechnology.eventuate.EventsourcedView$class.receiveEvent(EventsourcedView.scala:227) [eventuate-core_2.11-0.8.1.jar:0.8.1]
    at com.rbmhtechnology.eventuate.AbstractEventsourcedActor.com$rbmhtechnology$eventuate$ConfirmedDelivery$$super$receiveEvent(AbstractEventsourcedActor.scala:32) [eventuate-core_2.11-0.8.1.jar:0.8.1]
    at com.rbmhtechnology.eventuate.ConfirmedDelivery$class.receiveEvent(ConfirmedDelivery.scala:83) [eventuate-core_2.11-0.8.1.jar:0.8.1]
    at com.rbmhtechnology.eventuate.AbstractEventsourcedActor.com$rbmhtechnology$eventuate$PersistOnEvent$$super$receiveEvent(AbstractEventsourcedActor.scala:32) [eventuate-core_2.11-0.8.1.jar:0.8.1]
    at com.rbmhtechnology.eventuate.PersistOnEvent$class.receiveEvent(PersistOnEvent.scala:79) [eventuate-core_2.11-0.8.1.jar:0.8.1]
    at com.rbmhtechnology.eventuate.AbstractEventsourcedActor.receiveEvent(AbstractEventsourcedActor.scala:32) [eventuate-core_2.11-0.8.1.jar:0.8.1]
    at com.rbmhtechnology.eventuate.EventsourcedView$$anonfun$initiated$1.applyOrElse(EventsourcedView.scala:457) [eventuate-core_2.11-0.8.1.jar:0.8.1]
    at akka.actor.Actor$class.aroundReceive(Actor.scala:497) [akka-actor_2.11-2.4.17.jar:?]
    at com.rbmhtechnology.eventuate.AbstractEventsourcedView.aroundReceive(AbstractEventsourcedView.scala:55) [eventuate-core_2.11-0.8.1.jar:0.8.1]
    at akka.actor.ActorCell.receiveMessage(ActorCell.scala:526) [akka-actor_2.11-2.4.17.jar:?]
    at akka.actor.ActorCell.invoke(ActorCell.scala:495) [akka-actor_2.11-2.4.17.jar:?]
    at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:257) [akka-actor_2.11-2.4.17.jar:?]
    at akka.dispatch.Mailbox.run(Mailbox.scala:224) [akka-actor_2.11-2.4.17.jar:?]
    at akka.dispatch.Mailbox.exec(Mailbox.scala:234) [akka-actor_2.11-2.4.17.jar:?]
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) [scala-library-2.11.8.jar:?]
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) [scala-library-2.11.8.jar:?]
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) [scala-library-2.11.8.jar:?]
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) [scala-library-2.11.8.jar:?]

如何将一行与所有[i]的所有先前行值相乘?

1 个答案:

答案 0 :(得分:0)

您在寻找cumprod吗?

df$new <- cumprod(df$as)
df
#    as     new
# 1   6       6
# 2   7      42
# 3   3     126
# 4   4     504
# 5   8    4032
# 6  10   40320
# 7   1   40320
# 8   2   80640
# 9   9  725760
# 10  5 3628800