📌  相关文章
📜  R中两个日期之间的子集数据帧

📅  最后修改于: 2022-05-13 01:54:44.434000             🧑  作者: Mango

R中两个日期之间的子集数据帧

在本文中,我们将在 R 编程语言中获取 dataframe 中两个日期之间的数据。

我们可以使用 as.Date()函数创建一个列来存储日期

句法:

示例:创建数据框



R
# create a dataframe with 6 rows and 2 columns
# one column is of 6 dates with date type as
# year-month-day format
# second is temperature degrees
data = data.frame(date=c(as.Date('2020-6-6'),
                         as.Date('2021-12-1'),
                         as.Date('2021-11-27'),
                         as.Date('2020-6-1'),
                         as.Date('2019-6-6'),
                         as.Date('2017-12-1')),
                  temperature=c(43, 41, 34, 33, 40, 29))
  
# display
print(data)


R
# create a dataframe with 6 rows and 2 columns
# one column is of 6 dates with date type as 
# year-month-day format
# second is temperature degrees
data=data.frame(date=c(as.Date('2020-6-6'),
                       as.Date('2021-12-1'),
                       as.Date('2021-11-27'),
                       as.Date('2020-6-1'),
                       as.Date('2019-6-6'),
                       as.Date('2021-10-12')),
                temperature=c(43,41,34,33,40,29))
  
# subset the dataframe from 1 st january 2021 
# to 1 st October 2022
print(data[data$date > "2021-01-01" &   data$date < "2022-10-01", ])


R
# create a dataframe with 6 rows and 2 columns
# one column is of 6 dates with date type as
# year-month-day format
# second is temperature degrees
data=data.frame(date=c(as.Date('2020-6-6'),
                       as.Date('2021-12-1'),
                       as.Date('2021-11-27'),
                       as.Date('2020-6-1'),
                       as.Date('2019-6-6'),
                       as.Date('2017-10-12')),
                temperature=c(43,41,34,33,40,29))
  
# subset the dataframe from 11th november 2017 
# to 1 st October 2019
print(data[data$date > "2017-11-11" &   data$date < "2019-10-01", ])


输出:

我们将使用逻辑运算符对日期范围之间的数据进行子集化。因此,我们需要小于(<)大于(>)and(&)运算符。

句法:

在哪里,

  • 数据框是输入数据框
  • date_column 是 datfarame 中的日期列
  • date 是指定的日期,用于获取从 start_date 到 end_date 的数据

示例:从给定日期范围获取数据的 R 程序



电阻

# create a dataframe with 6 rows and 2 columns
# one column is of 6 dates with date type as 
# year-month-day format
# second is temperature degrees
data=data.frame(date=c(as.Date('2020-6-6'),
                       as.Date('2021-12-1'),
                       as.Date('2021-11-27'),
                       as.Date('2020-6-1'),
                       as.Date('2019-6-6'),
                       as.Date('2021-10-12')),
                temperature=c(43,41,34,33,40,29))
  
# subset the dataframe from 1 st january 2021 
# to 1 st October 2022
print(data[data$date > "2021-01-01" &   data$date < "2022-10-01", ])

输出:

示例:从给定日期范围获取数据的 R 程序

电阻

# create a dataframe with 6 rows and 2 columns
# one column is of 6 dates with date type as
# year-month-day format
# second is temperature degrees
data=data.frame(date=c(as.Date('2020-6-6'),
                       as.Date('2021-12-1'),
                       as.Date('2021-11-27'),
                       as.Date('2020-6-1'),
                       as.Date('2019-6-6'),
                       as.Date('2017-10-12')),
                temperature=c(43,41,34,33,40,29))
  
# subset the dataframe from 11th november 2017 
# to 1 st October 2019
print(data[data$date > "2017-11-11" &   data$date < "2019-10-01", ])

输出: