如何在R中将日期转换为阴历日期? [英] How to convert date to lunar date in R?

查看:118
本文介绍了如何在R中将日期转换为阴历日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有渔业数据集(



我们还可以填写缺少的日期,从而使农历周期更加明显:

  all_dates<-data.frame(Date = seq.Date(min(as.Date(sample_data $ fdate, %d /%m /%Y)),
max(as.Date(sample_data $ fdate,%d / %m /%Y)),
= = 1天))%&%;%
变异(illum = lunar.illumination.mean(Date))

all_dates%>%
ggplot(aes(Date,illum))+ geom_point()



现在,假设您的数据集具有名为 catch ,我们可以通过将渔获量数据与整个日期范围结合起来,然后绘制渔获量和月球照度来开始分析。此数据集还可用于回归,相关性等。

 #模拟捕获数据
set.seed(123)
sample_data<-sample_data%&%;%
mutate(catch = rnorm(16,100,30))

all_dates%>
left_join(mutate(sample_data,Date = as.Date(fdate,%d /%m /%Y))))%&%;%
select(Date,illum,catch)%> %
收集(变量,值,-日期)%&%;%
ggplot(aes(日期,值))+
geom_point()+
facet_grid(变量〜。, scales = free_y)


I have fisheries data set ( sample data set) . I'm going to study moons impact on the fish catch. I used lunar package to find moon phase of each fishing day.

library(lunar)
data$lunar_phase <- lunar.phase(as.Date(data$fdate))

output as follows

fdate   lunar_phase
29/3/2006   3.51789248
28/3/2006   1.255536876
24/3/2006   4.559716361
26/3/2006   2.801242263
25/3/2006   0.538886659

lunar package can be used to categorize lunar phase into 4 or 8 periods.

I need to convert the fishing date to relative lunar cycle date. Lunar cycle is 29.53 days. If lunar day 0 = full moon, then find lunar cycle dates of other dates.

Is there any possible way to do that?

Expected output may be as follows

fdate   lunar_day
29/3/2006   6
28/3/2006   4
24/3/2006   10
26/3/2006   5
25/3/2006   1

解决方案

I don't know of a package that calculates "lunar day" from a date. In theory you could do it from your dataset, by identifying the maximum and minimum values for phase, then converting phases to percentages and rounding up as a proportion of 29.53.

However, the lunar package also calculates illumination (as a fraction of visible surface). I think this is a good proxy for lunar day and also gives you a physical value, rather than something more arbitrary.

Using your data, it's clear that new moon occurs near the start of the month:

library(tidyverse)
library(lunar)
sample_data <- read_csv("sample_data.csv")
sample_data %>% 
  mutate(Date = as.Date(fdate, "%d/%m/%Y"), 
         illum = lunar.illumination.mean(Date)) %>% 
  ggplot(aes(Date, illum)) + geom_point()

We could also fill in the missing dates, which makes the lunar cycle apparent:

all_dates <- data.frame(Date = seq.Date(min(as.Date(sample_data$fdate, "%d/%m/%Y")), 
                                        max(as.Date(sample_data$fdate, "%d/%m/%Y")), 
                                        by = "1 day")) %>% 
  mutate(illum = lunar.illumination.mean(Date)) 

all_dates %>%
  ggplot(aes(Date, illum)) + geom_point()

Now, assuming your dataset has a column named catch, we could begin analysis by joining the catch data with the full date range, then plotting catch and lunar illumination. This dataset may also be used for regression, correlation etc.

# simulated catch data
set.seed(123)
sample_data <- sample_data %>% 
  mutate(catch = rnorm(16, 100, 30))

all_dates %>% 
  left_join(mutate(sample_data, Date = as.Date(fdate, "%d/%m/%Y"))) %>% 
  select(Date, illum, catch) %>% 
  gather(variable, value, -Date) %>% 
  ggplot(aes(Date, value)) + 
    geom_point() + 
    facet_grid(variable~., scales = "free_y")

这篇关于如何在R中将日期转换为阴历日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆