如何在R中将一个tsibble列除以一个数字?

huangapple go评论117阅读模式
英文:

How to divide a tsibble column by a number in R?

问题

我之前发布了一个类似的问题,并得到了一个适用于xts的答案:

  1. standardize<-function(ts) { as.xts(apply(ts, 2, function(x) x / x[1])) }

现在我有一个tsibble,在执行时出现了错误:

  1. Error in x/x[1] : non-numeric argument to binary operator
  1. require(fpp3)
  2. goog<-gafa_stock|>filter(Symbol=="GOOG",year(Date)==2018)
  3. close<-goog|>select(Close)
  4. close1 <- apply(close, 2, function(x) x / x[1])
英文:

I posted a similar question a while ago and got an anwser that worked for an xts:

  1. standardize&lt;-function(ts) { as.xts(apply(ts, 2, function(x) x / x[1])) }

Now I have a tsibble that fails with a:

  1. Error in x/x[1] : non-numeric argument to binary operator
  2. require(fpp3)
  3. goog&lt;-gafa_stock|&gt;filter(Symbol==&quot;GOOG&quot;,year(Date)==2018)
  4. close&lt;-goog|&gt;select(Close)
  5. close1 &lt;- apply(close, 2, function(x) x / x[1])

答案1

得分: 1

我们可以这样做。apply 需要一个数组,包括一个矩阵。我们可以使用 mutate 结合 group_by() 使用 Close = Close / first(Close)

  1. require(fpp3)
  2. gafa_stock %>%
  3. filter(Symbol == "GOOG", year(Date) == 2018) %>%
  4. #group_by(Symbol) %>%
  5. mutate(Close = Close / first(Close))
  1. # 一个时间序列表格: 251 x 8 [!]
  2. # 关键词: Symbol [1]
  3. # 分组: Symbol [1]
  4. Symbol Date Open High Low Close Adj_Close Volume
  5. <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
  6. 1 GOOG 2018-01-02 1048. 1067. 1045. 1 1065 1237600
  7. 2 GOOG 2018-01-03 1064. 1086. 1063. 1.02 1082. 1430200
  8. 3 GOOG 2018-01-04 1088 1094. 1084. 1.02 1086. 1004600
  9. 4 GOOG 2018-01-05 1094 1104. 1092 1.03 1102. 1279100
  10. 5 GOOG 2018-01-08 1102. 1111. 1102. 1.04 1107. 1047600
  11. 6 GOOG 2018-01-09 1109. 1111. 1101. 1.04 1106. 902500
  12. 7 GOOG 2018-01-10 1097. 1105. 1096. 1.04 1103. 1042800
  13. 8 GOOG 2018-01-11 1106. 1107. 1100. 1.04 1106. 978300
  14. 9 GOOG 2018-01-12 1102. 1124. 1101. 1.05 1122. 1720500
  15. 10 GOOG 2018-01-16 1133. 1140. 1118. 1.05 1122. 1575300
  16. # … 还有 241 行数据
英文:

We can do it this way. apply expects an array, including a matrix. We can use mutate with group_by() and use Close = Close / first(Close):

  1. require(fpp3)
  2. gafa_stock %&gt;%
  3. filter(Symbol==&quot;GOOG&quot;, year(Date)==2018) %&gt;%
  4. #group_by(Symbol) %&gt;%
  5. mutate(Close = Close / first(Close))
  1. # A tsibble: 251 x 8 [!]
  2. # Key: Symbol [1]
  3. # Groups: Symbol [1]
  4. Symbol Date Open High Low Close Adj_Close Volume
  5. &lt;chr&gt; &lt;date&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
  6. 1 GOOG 2018-01-02 1048. 1067. 1045. 1 1065 1237600
  7. 2 GOOG 2018-01-03 1064. 1086. 1063. 1.02 1082. 1430200
  8. 3 GOOG 2018-01-04 1088 1094. 1084. 1.02 1086. 1004600
  9. 4 GOOG 2018-01-05 1094 1104. 1092 1.03 1102. 1279100
  10. 5 GOOG 2018-01-08 1102. 1111. 1102. 1.04 1107. 1047600
  11. 6 GOOG 2018-01-09 1109. 1111. 1101. 1.04 1106. 902500
  12. 7 GOOG 2018-01-10 1097. 1105. 1096. 1.04 1103. 1042800
  13. 8 GOOG 2018-01-11 1106. 1107. 1100. 1.04 1106. 978300
  14. 9 GOOG 2018-01-12 1102. 1124. 1101. 1.05 1122. 1720500
  15. 10 GOOG 2018-01-16 1133. 1140. 1118. 1.05 1122. 1575300
  16. # … with 241 more rows

huangapple
  • 本文由 发表于 2023年6月9日 11:27:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76437020.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定