如何将每日时间序列转换为平均每周?(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
最满意答案
基本上你有两种方法来获得整数的所有部分的总和。
用数字操作
拿这个数字,并建立剩下的十个并添加它。 然后把数字的整数部分除以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"/>
相关问答
更多-
length是属性,而不是一种方法。 你不能调用它,因此你不需要括号() : function getlength(number) { return number.toString().length; } 更新:正如在评论中讨论的,上面的例子将不适用于浮点数。 为了使它工作,我们可以使用String(number).replace('.', '').length ,或使用正则表达式对数字进行计数: String(number).match(/\d/g).length 。 在速度方面,可以通过数学方 ...
-
如果一个“暴力”方法是可行的,我首先将P(n)定义为一个函数(见下面的函数),并使用for对其进行求和: function p(inputN) { var n = inputN, m = 1, digit; while (n) { digit = n % 10; if (digit) m *= digit; n = Math.floor(n / 10); } return m; } function result3() { var sum = ...
-
您的问题实际上涉及两个不同的问题 第一个问题解决起来非常简单并且已经得到了解答:如果您只想打印最高的总和,那么从降序排序的结果中您只需要打印第一个项目,而不是整个数组。 但微妙的是你想要后者的最高金额,如果不止一个。 目前你无法确保这一点:由于JS Array.prototype.sort()函数不稳定,你无法知道ex-aequo将会以何种顺序出现。 要修复它,你不仅要按总和而且按等级排序,如下所示: function LargestPhoneNumber (array) { var i = ...
-
数字的所有数字的总和(Sum of all digits of a number)[2022-05-02]
对于新数组,可以使用String#match而不是String#split 。 function sumDigits(num) { return num.toString().match(/-?\d/g).reduce(function(a, b) { return +a + +b; }); } console.log(sumDigits(1148)); // 14 console.log(sumDigits(-316)); // 4 You could us ... -
基本上你有两种方法来获得整数的所有部分的总和。 用数字操作 拿这个数字,并建立剩下的十个并添加它。 然后把数字的整数部分除以10.继续。 var value = 2568, sum = 0; while (value) { sum += value % 10; value = Math.floor(value / 10); } console.log(sum); 使用字符串操作 将数字转换为字符串,拆分字符串并获取包含所有数字的数组,并为每个部分执行缩减并返回总和 ...
-
一个数字的总和(sum of digits of a number)[2024-02-16]
这里不需要字符串函数。 你可以简单地在mod( %操作)的帮助下得到结果。 function sumDigits(number) { document.querySelector('h1').innerHTML = ((number-1)%9+1); } There is no need of string functions here. You can simply arrive the result with help of mod(% operation. function sumDig ... -
我想用小提琴或Rextester来测试这个,但是现在都不行。 所以,你的upvotes / downvotes将作为测试: SELECT CAST(SUBSTRING(number, 1, 1) AS UNSIGNED) + -- first digit CAST(SUBSTRING(number, 2, 1) AS UNSIGNED) + -- second digit CAST(SUBSTRING(number, 3, 1) ...
-
在python中测试一个数字中数字的总和是否相等?(Testing the equality of the sum of a digits within a number on python?)[2022-02-26]
我认为这是诀窍。 虽然不确定: def f(n): s = str(n) l = len(s) for n in (n for n in range(1, l + 1) if l % n == 0): ints = [int(''.join(x)) for x in zip(*[iter(s)]*n)] if sum(ints) == 100: return True return False zip东西来自这里 。 ... -
另一种方法是通过使用模数来遍历数字。 我不习惯使用语法,但是这里有一个函数可以在Lisp中用于非负整数(并且有一些工作可以包含小数和负值): (defun sum-of-digits(x) (if (= x 0) 0 (+ (mod x 10) (sum-of-digits (/ (- x (mod x 10)) 10))))) An alternative method would be to loop over the digits by using modul ...
-
如果要求的和是s ,那么每一半必须有和s/2 。 现在,你需要找到f(n, s/2) :有多少个n位数字的位数为s/2 。 知道f(n, s/2) ,你可以在一行中得到答案(试着自己搞清楚)。 至于如何计算f(n, m) :这是标准DP。 你有递归公式,如f(n, m) = f(n-1, m) + f(n-1, m-1) + f(n-1, m-2) + ... + f(n-1, m-9) 。 这里, 0, 1, 2, .. 9都是给定数字的最后一位数的可能选项。 如果最后一个数字是k ,则其余数字是(n-1 ...