将时间序列中的列按其第一个值在R中分割。

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

How to divide the columns in a time series by their first value in R?

问题

以下是代码的中文翻译部分:

  1. 我已经让下面的代码工作了。但一定有更好的方法。
  2. file <- "http://s3.amazonaws.com/assets.datacamp.com/production/course_1127/datasets/tmp_file.csv"
  3. x <- read.csv(file = file)
  4. ts <- xts(x = x, order.by = as.Date(rownames(x), "%m/%d/%Y"))
  5. cd=coredata(ts)
  6. for (j in 1:length(names(ts))) cd[,j]<-cd[,j]/cd[1,j]
  7. for (j in 1:length(names(ts))) ts[,j]<-cd[,j]
英文:

I have the code below working. But there must be a better way.

  1. file &lt;- &quot;http://s3.amazonaws.com/assets.datacamp.com/production/course_1127/datasets/tmp_file.csv&quot;
  2. x &lt;- read.csv(file = file)
  3. ts &lt;- xts(x = x, order.by = as.Date(rownames(x), &quot;%m/%d/%Y&quot;))
  4. cd=coredata(ts)
  5. for (j in 1:length(names(ts))) cd[,j]&lt;-cd[,j]/cd[1,j]
  6. for (j in 1:length(names(ts))) ts[,j]&lt;-cd[,j]

答案1

得分: 2

你可以通过重复所需的缩放行来创建一个矩阵,然后将原始数据除以该缩放矩阵:

  1. > ts <- xts(x = x, order.by = as.Date(rownames(x), "%m/%d/%Y"))
  2. > ts
  3. a b
  4. 2015-01-02 1 3
  5. 2015-02-03 2 4
  6. > m <- matrix(rep(coredata(ts)[1,], nrow(ts)), ncol = 2, byrow = TRUE)
  7. > m
  8. [,1] [,2]
  9. [1,] 1 3
  10. [2,] 1 3
  11. > coredata(ts) <- coredata(ts) / m
  12. > ts
  13. a b
  14. 2015-01-02 1 1.00000
  15. 2015-02-03 2 1.33333
  16. >

这很简洁,但可能会占用更多内存。 @TarJae 的回答可能更节省内存,我已经点赞了。

英文:

You can create a matrix from your desired scaling row by repeating the row, and then divide the original data by that scaling matrix:

  1. &gt; ts &lt;- xts(x = x, order.by = as.Date(rownames(x), &quot;%m/%d/%Y&quot;))
  2. &gt; ts
  3. a b
  4. 2015-01-02 1 3
  5. 2015-02-03 2 4
  6. &gt; m &lt;- matrix(rep(coredata(ts)[1,], nrow(ts)), ncol = 2, byrow = TRUE)
  7. &gt; m
  8. [,1] [,2]
  9. [1,] 1 3
  10. [2,] 1 3
  11. &gt; coredata(ts) &lt;- coredata(ts) / m
  12. &gt; ts
  13. a b
  14. 2015-01-02 1 1.00000
  15. 2015-02-03 2 1.33333
  16. &gt;

That is concise, but the expense of memory. The answer by @TarJae is as nice but possiblt memory efficient so I already upvoted it.

答案2

得分: 1

我们可以使用 apply 将每列除以其第一个值:

  1. file <- "http://s3.amazonaws.com/assets.datacamp.com/production/course_1127/datasets/tmp_file.csv"
  2. x <- read.csv(file = file)
  3. library(xts)
  4. ts <- xts(x = x, order.by = as.Date(rownames(x), "%m/%d/%Y"))
  5. ts <- apply(ts, 2, function(x) x / x[1])
  6. a b
  7. 2015-01-02 1 1.000000
  8. 2015-02-03 2 1.333333
英文:

We can use apply to divide each column by its first value:

  1. file &lt;- &quot;http://s3.amazonaws.com/assets.datacamp.com/production/course_1127/datasets/tmp_file.csv&quot;
  2. x &lt;- read.csv(file = file)
  3. library(xts)
  4. ts &lt;- xts(x = x, order.by = as.Date(rownames(x), &quot;%m/%d/%Y&quot;))
  5. ts &lt;- apply(ts, 2, function(x) x / x[1])
  6. a b
  7. 2015-01-02 1 1.000000
  8. 2015-02-03 2 1.333333

huangapple
  • 本文由 发表于 2023年5月8日 00:18:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195019.html
匿名

发表评论

匿名网友

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

确定