获取达到所需金额所需的天数
我目前有一个数据框,其中包含站点名称、降雨日期、降雨量(随附示例),我有兴趣探索每个站点达到特定降雨量所需的天数(和/或月数)。 例如:
是否有可能基于像示例这样的数据集获得像上面这样的输出? 我最初的思考过程是单独过滤每个站点,加入一个日历数据框,从该范围中提取最小值和最大值,计算它们之间的天数,并使用 case_when 对它们进行分类。这种方法似乎有点复杂,希望得到任何有关更好方法的指导。
感谢您的建议!
示例数据集:
Example <- structure(list(Name.Station = c("Station A", "Station A", "Station A",
"Station A", "Station A", "Station B", "Station B", "Station B",
"Station C", "Station C", "Station C", "Station C"), Rainfall.Date = c("7/10/2020",
"8/12/2020", "8/01/2021", "25/06/2021", "26/10/2021", "7/01/2020",
"22/01/2020", "5/02/2020", "5/09/2020", "5/10/2020", "5/11/2020",
"5/12/2020"), Rainfall.Amount = c(210, 210, 208.47, 208.16, 203.67,
227.49, 225, 222.54, 250, 250, 246.18, 245.15)), class = "data.frame", row.names = c(NA,
-12L))
I currently have a data frame which contains Name of Station, Date of Rainfall, Amount of Rainfall (Example attached) I am interested in exploring the number of days (and/or months) it takes each Station to reach a particular amount of rainfall.
For example:
Is it possible to obtain an output like the one above based on a dataset like the example one?
My initial thought process is to filter each station individually, join to a calendar dataframe which extracts the min and max from that range, count the days between them and use a case_when to categorize them. This approach seems a bit convoluted and would appreciate any guidance as to what would be a better approach.
Thanks for the suggestions!
Example Dataset:
Example <- structure(list(Name.Station = c("Station A", "Station A", "Station A",
"Station A", "Station A", "Station B", "Station B", "Station B",
"Station C", "Station C", "Station C", "Station C"), Rainfall.Date = c("7/10/2020",
"8/12/2020", "8/01/2021", "25/06/2021", "26/10/2021", "7/01/2020",
"22/01/2020", "5/02/2020", "5/09/2020", "5/10/2020", "5/11/2020",
"5/12/2020"), Rainfall.Amount = c(210, 210, 208.47, 208.16, 203.67,
227.49, 225, 222.54, 250, 250, 246.18, 245.15)), class = "data.frame", row.names = c(NA,
-12L))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过
站,您可以计算出大于阈值的降雨量cumsum
(以毫米为单位)。然后计算从开始日期到累计和中最大值的日期的序列
天数的长度
。不过,首先,您的日期格式应该正确。
注意:使用 R >= 4.1。
by
station you could calculate thecumsum
of rainfall greater than the threshold in mm. Then calculate thelength
of theseq
uence of days from start date to the date which is maximum in the cumsum.First of all, though, your dates should be formatted properly.
Note: R >= 4.1 used.
这是一个
tidyverse
方法:Here is a
tidyverse
approach: