如何使用 R 显示日期范围
How to show the Date Range using R
我对使用 R
真的很陌生。我正在尝试用数据集做一些基本的事情。我有一个只有一列 (date
) 的数据集,其中包含参与者填写调查的日期:
11/11/2015
11/11/2015
11/11/2015
11/11/2015
11/11/2015
11/11/2015
11/11/2015
11/11/2015
27/11/2015
27/11/2015
etc (there are about 180 more responses)
到目前为止我已经做了:
NUdates <- nrow(unique(date))
这表明根据回复有 12 个不同的日期
接下来我要做的只是 return 最早和最晚的日期,所以我会有对象:
Emonth # (this would be the earliest month that a participant filled out the survey)
Lmonth # (this would be the latest month that a participant filled out he survey)
Year # (this would be the year the surveys were filled out)
然后使用 markdown 我可以说:
participants completed surveys on r (NUdates)
days between r (Emonth)
and r (Lmonth)
in r (year)
.
txt <- "11/11/2015
11/11/2015
11/11/2015
11/11/2015
11/11/2015
11/11/2015
11/11/2015
11/11/2015
27/11/2015
27/11/2015"
> dates <- as.Date(scan(text=txt, what=""), format="%d/%m/%Y")
Read 10 items
> dates
[1] "2015-11-11" "2015-11-11" "2015-11-11" "2015-11-11" "2015-11-11"
[6] "2015-11-11" "2015-11-11" "2015-11-11" "2015-11-27" "2015-11-27"
)
要获得 Date class 对象之间的差异,可以使用 -
运算符。
> max(dates) - min(dates)
Time difference of 16 days
要删除多余的 material,您可以使用 unclass
:
dput(max(dates) - min(dates))
structure(0, units = "days", class = "difftime")
unclass(max(dates) - min(dates))
[1] 16
attr(,"units")
[1] "days"
此答案假设您在序列中找到了您的唯一日期。
后续步骤 -
1) 确保您的日期已排序。您可以通过 -
table <- table[order(table$Date),]
table 是您唯一日期的数据框。
2) 使用包 timeDate
您需要函数 timeFirstDayInMonth() 和 timeLastDayInMonth()。
start_date <- timeFirstDayInMonth(table$Date[1]) #First Date Of First Occuring Month
end_date <- timeLastDayInMonth(table$Date[nrow(table)-1]) #Last Date Of Last Occuring Month
3)终于可以找到Emonth和Lmonth了
Emonth <- format(start_date, format = "%B") #Full Month name
或
Lmonth <- format(end_date, format = "%m") #Decimal Month
我对使用 R
真的很陌生。我正在尝试用数据集做一些基本的事情。我有一个只有一列 (date
) 的数据集,其中包含参与者填写调查的日期:
11/11/2015
11/11/2015
11/11/2015
11/11/2015
11/11/2015
11/11/2015
11/11/2015
11/11/2015
27/11/2015
27/11/2015
etc (there are about 180 more responses)
到目前为止我已经做了:
NUdates <- nrow(unique(date))
这表明根据回复有 12 个不同的日期
接下来我要做的只是 return 最早和最晚的日期,所以我会有对象:
Emonth # (this would be the earliest month that a participant filled out the survey)
Lmonth # (this would be the latest month that a participant filled out he survey)
Year # (this would be the year the surveys were filled out)
然后使用 markdown 我可以说:
participants completed surveys on
r (NUdates)
days betweenr (Emonth)
andr (Lmonth)
inr (year)
.
txt <- "11/11/2015
11/11/2015
11/11/2015
11/11/2015
11/11/2015
11/11/2015
11/11/2015
11/11/2015
27/11/2015
27/11/2015"
> dates <- as.Date(scan(text=txt, what=""), format="%d/%m/%Y")
Read 10 items
> dates
[1] "2015-11-11" "2015-11-11" "2015-11-11" "2015-11-11" "2015-11-11"
[6] "2015-11-11" "2015-11-11" "2015-11-11" "2015-11-27" "2015-11-27"
)
要获得 Date class 对象之间的差异,可以使用 -
运算符。
> max(dates) - min(dates)
Time difference of 16 days
要删除多余的 material,您可以使用 unclass
:
dput(max(dates) - min(dates))
structure(0, units = "days", class = "difftime")
unclass(max(dates) - min(dates))
[1] 16
attr(,"units")
[1] "days"
此答案假设您在序列中找到了您的唯一日期。
后续步骤 -
1) 确保您的日期已排序。您可以通过 -
table <- table[order(table$Date),]
table 是您唯一日期的数据框。
2) 使用包 timeDate
您需要函数 timeFirstDayInMonth() 和 timeLastDayInMonth()。
start_date <- timeFirstDayInMonth(table$Date[1]) #First Date Of First Occuring Month
end_date <- timeLastDayInMonth(table$Date[nrow(table)-1]) #Last Date Of Last Occuring Month
3)终于可以找到Emonth和Lmonth了
Emonth <- format(start_date, format = "%B") #Full Month name
或
Lmonth <- format(end_date, format = "%m") #Decimal Month