我有一个.txt文件
test.txt
V1 V2 Date
A B 2020-01-02
C D 2020-02-27
E F 2020-09-10
G H 2020-09-15
我想根据最近一个月对数据进行子集化。我这样做可以完成工作,但是我想自动提取最近的一个月,而不是手动输入然后提取数据
test$month <- factor(format(test$Date, "%B"),levels = month.name)
test.subset <- test[test$month == "September"]
我们可以arrange
在Date
类列,filter
将format
通过与比较ED值last
一个
library(dplyr)
test %>%
mutate(Date = as.Date(Date), Month = format(Date, '%B')) %>%
arrange(Date) %>%
filter(Month == last(Month)) %>%
select(-Month)
-输出
# V1 V2 Date
#1 E F 2020-09-10
#2 G H 2020-09-15
test <- structure(list(V1 = c("A", "C", "E", "G"), V2 = c("B", "D", "F",
"H"), Date = c("2020-01-02", "2020-02-27", "2020-09-10", "2020-09-15"
)), class = "data.frame", row.names = c(NA, -4L))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句