首页 \ 问答 \ 如何将每日时间序列转换为平均每周?(How to convert a daily times series into an averaged weekly?)

如何将每日时间序列转换为平均每周?(How to convert a daily times series into an averaged weekly?)

我希望(算术上)平均每日数据,从而将我的每日时间序列转换为每周一次。

遵循这个主题: 如何使用R计算每列数据的平均值? ,我正在使用xts库。

# Averages daily time series into weekly time series
# where my source is a zoo object
source.w <- apply.weekly(source, colMeans)

我遇到的问题是,这个系列的平均值是星期二到下一个星期一的数据。

我正在寻找从星期一到星期五平均每日数据的选项。

任何提示?

这里有一点:

    # here is part of my data, from a "blé colza.txt" file


    24/07/2012  250.5   499
    23/07/2012  264.75  518.25
    20/07/2012  269.25  525.25
    19/07/2012  267 522.5
    18/07/2012  261.25  517
    17/07/2012  265.75  522.25
    16/07/2012  264.25  523.25
    13/07/2012  258.25  517
    12/07/2012  253.75  513
    11/07/2012  246.25  512.75
    10/07/2012  248 515
    09/07/2012  247 519.25
    06/07/2012  243.25  508.25
    05/07/2012  245 508.5
    04/07/2012  236 500.5
    03/07/2012  234 497.75
    02/07/2012  234.25  489.75
    29/06/2012  229 490.25
    28/06/2012  229.75  487.25
    27/06/2012  229.75  493
    26/06/2012  226.5   486
    25/06/2012  220 482.25
    22/06/2012  214.25  472.5
    21/06/2012  212 469.5
    20/06/2012  210.25  473.75
    19/06/2012  208 472.75
    18/06/2012  206.75  462.5
    15/06/2012  203 456.5
    14/06/2012  205.25  460.5
    13/06/2012  205.25  465.25
    12/06/2012  205.25  469
    11/06/2012  208 471.5
    08/06/2012  208 468.5
    07/06/2012  208 471.25
    06/06/2012  208 467
    05/06/2012  208 458.75
    04/06/2012  208 457.5
    01/06/2012  208 463.5
    31/05/2012  208 466.75
    30/05/2012  208 468
    29/05/2012  212.75  469.75
    28/05/2012  212.75  469.75
    25/05/2012  212.75  465.5



# Loads external libraries
library("zoo") # or require("zoo")
library("xts") # or require("xts")

# Loads data as a zoo object
source <- read.zoo("blé colza.txt", sep=",", dec=".", header=T, na.strings="NA",     format="%d/%m/%Y")

# Averages daily time series into weekly time series
# https://stackoverflow.com/questions/11129562/how-does-one-compute-the-mean-of-weekly-    data-by-column-using-r
source.w <- apply.weekly(source, colMeans) 

I am wishing to (arithmetically) average daily data and thus convert my daily time series into a weekly one.

Following this thread: How does one compute the mean of weekly data by column using R? , I am using the xts library.

# Averages daily time series into weekly time series
# where my source is a zoo object
source.w <- apply.weekly(source, colMeans)

The problem I am having is that it averages the series taking tuesday through next monday data.

I am searching for options to average my daily data from monday through friday.

Any hints?

Here is a bit more:

    # here is part of my data, from a "blé colza.txt" file


    24/07/2012  250.5   499
    23/07/2012  264.75  518.25
    20/07/2012  269.25  525.25
    19/07/2012  267 522.5
    18/07/2012  261.25  517
    17/07/2012  265.75  522.25
    16/07/2012  264.25  523.25
    13/07/2012  258.25  517
    12/07/2012  253.75  513
    11/07/2012  246.25  512.75
    10/07/2012  248 515
    09/07/2012  247 519.25
    06/07/2012  243.25  508.25
    05/07/2012  245 508.5
    04/07/2012  236 500.5
    03/07/2012  234 497.75
    02/07/2012  234.25  489.75
    29/06/2012  229 490.25
    28/06/2012  229.75  487.25
    27/06/2012  229.75  493
    26/06/2012  226.5   486
    25/06/2012  220 482.25
    22/06/2012  214.25  472.5
    21/06/2012  212 469.5
    20/06/2012  210.25  473.75
    19/06/2012  208 472.75
    18/06/2012  206.75  462.5
    15/06/2012  203 456.5
    14/06/2012  205.25  460.5
    13/06/2012  205.25  465.25
    12/06/2012  205.25  469
    11/06/2012  208 471.5
    08/06/2012  208 468.5
    07/06/2012  208 471.25
    06/06/2012  208 467
    05/06/2012  208 458.75
    04/06/2012  208 457.5
    01/06/2012  208 463.5
    31/05/2012  208 466.75
    30/05/2012  208 468
    29/05/2012  212.75  469.75
    28/05/2012  212.75  469.75
    25/05/2012  212.75  465.5



# Loads external libraries
library("zoo") # or require("zoo")
library("xts") # or require("xts")

# Loads data as a zoo object
source <- read.zoo("blé colza.txt", sep=",", dec=".", header=T, na.strings="NA",     format="%d/%m/%Y")

# Averages daily time series into weekly time series
# https://stackoverflow.com/questions/11129562/how-does-one-compute-the-mean-of-weekly-    data-by-column-using-r
source.w <- apply.weekly(source, colMeans) 

原文:https://stackoverflow.com/questions/11892063
更新时间:2022-08-02 22:08

最满意答案

基本上你有两种方法来获得整数的所有部分的总和。

  • 用数字操作

    拿这个数字,并建立剩下的十个并添加它。 然后把数字的整数部分除以10.继续。

var value = 2568,
    sum = 0;

while (value) {
    sum += value % 10;
    value = Math.floor(value / 10);
}

console.log(sum);

  • 使用字符串操作

    将数字转换为字符串,拆分字符串并获取包含所有数字的数组,并为每个部分执行缩减并返回总和。

var value = 2568,
    sum = value
        .toString()
        .split('')
        .map(Number)
        .reduce(function (a, b) {
            return a + b;
        }, 0);

console.log(sum);


要返回值,您需要添加value属性。

rezultat.value = sum;
//      ^^^^^^

function sumDigits() {
    var value = document.getElementById("thenumber").value,
        sum = 0;

  while (value) {
      sum += value % 10;
      value = Math.floor(value / 10);
  }
  
  var rezultat = document.getElementById("result");
  rezultat.value = sum;
}
<input type="text" placeholder="number" id="thenumber"/><br/><br/>
<button onclick="sumDigits()">Calculate</button><br/><br/>
<input type="text" readonly="true" placeholder="the result" id="result"/>


Basically you have two methods to get the sum of all parts of an integer number.

  • With numerical operations

    Take the number and build the remainder of ten and add that. Then take the integer part of the division of the number by 10. Proceed.

var value = 2568,
    sum = 0;

while (value) {
    sum += value % 10;
    value = Math.floor(value / 10);
}

console.log(sum);

  • Use string operations

    Convert the number to string, split the string and get an array with all digits and perform a reduce for every part and return the sum.

var value = 2568,
    sum = value
        .toString()
        .split('')
        .map(Number)
        .reduce(function (a, b) {
            return a + b;
        }, 0);

console.log(sum);


For returning the value, you need to addres the value property.

rezultat.value = sum;
//      ^^^^^^

function sumDigits() {
    var value = document.getElementById("thenumber").value,
        sum = 0;

  while (value) {
      sum += value % 10;
      value = Math.floor(value / 10);
  }
  
  var rezultat = document.getElementById("result");
  rezultat.value = sum;
}
<input type="text" placeholder="number" id="thenumber"/><br/><br/>
<button onclick="sumDigits()">Calculate</button><br/><br/>
<input type="text" readonly="true" placeholder="the result" id="result"/>

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)