楼主: jiqimao742
1645 0

[学习分享] 使用@lubridate@轻松处理日期和时间dates and times [推广有奖]

  • 1关注
  • 4粉丝

已卖:25份资源

大专生

50%

还不是VIP/贵宾

-

威望
0
论坛币
275 个
通用积分
1.0000
学术水平
2 点
热心指数
7 点
信用等级
4 点
经验
1684 点
帖子
47
精华
0
在线时间
41 小时
注册时间
2016-9-28
最后登录
2022-2-26

楼主
jiqimao742 发表于 2016-10-6 16:34:21 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
Dates and Times – Simple and Easy with lubridate exercises (part 1)
转自 R-Excecise 论坛的一篇文章, 针对lubricate包的练习Dates and Times – Simple and Easy with lubridate exercises (part 1)
Parsing Date-times


The ymd() series of functions are used to parse character strings into dates.
The letters y, m, and d correspond to the year, month, and day elements of a date-time.

Exercise 1
Populate a variable called “start_date” with a date representation of string “23012017”

Exercise 2
Use the lubridate function today to print the current date

Exercise 3
Extract the year part from the “start_date” variable created on exercise 1

Exercise 4
Extract the month part from the “start_date” variable created on exercise 1

Exercise 5
Extract the day part from the “start_date” variable created on exercise 1

Exercise 6
Set the month in variable “start_date” to February

Exercise 7
Add 6 days to variable “start_date”.
Did you notice what happened to the month value?

Exercise 8
Substract 3 months from variable “start_date”

Exercise 9 (Advanced)
Populate a field called concatenated_dates with a vector of dates containing the following values:
“31.12.2015”, “01.01.2016”, “15.02.2016”

Exercise 10 (Advanced)
Calculate in a short and simple way the addition of 1 thru 10 days to “start_date” variable

答案

Below are the solutions to these exercises on dates and times – Simple lubridate Exercises.

#####################                  ##    Exercise 1    ##                  #####################start_date<- dmy("23012017")start_date
## [1] "2017-01-23"
#####################                  ##    Exercise 2    ##                  #####################today()
## [1] "2016-08-14"
#####################                  ##    Exercise 3    ##                  #####################year(start_date)
## [1] 2017
#####################                  ##    Exercise 4    ##                  #####################month(start_date)
## [1] 1
#####################                  ##    Exercise 5    ##                  #####################day(start_date)
## [1] 23
#####################                  ##    Exercise 6    ##                  #####################month(start_date) <- 2#####################                  ##    Exercise 7    ##                  #####################start_date + days(6)
## [1] "2017-03-01"
#####################                  ##    Exercise 8    ##                  #####################start_date - months(3)
## [1] "2016-11-23"
#####################                  ##    Exercise 9    ##                  #####################concatenated_dates <- dmy(c("31.12.2015", "01.01.2016",  "15.02.2016"))concatenated_dates
## [1] "2015-12-31" "2016-01-01" "2016-02-15"
#####################                  ##    Exercise 10   ##                  #####################start_date + c(1:10) * days(1)
##  [1] "2017-02-24" "2017-02-25" "2017-02-26" "2017-02-27" "2017-02-28"##  [6] "2017-03-01" "2017-03-02" "2017-03-03" "2017-03-04" "2017-03-05"



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Dates Times date time Rida character exercises elements letters called

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-30 22:25