其他分享
首页 > 其他分享> > R语言中根据日期筛选数据

R语言中根据日期筛选数据

作者:互联网

 

 

R语言中根据日期筛选数据

1、测试数据

manager <- 1:5
date <- c("10/24/08","10/28/08","10/1/08","10/12/08","5/1/09")
country <- c("US","US","UK","UK","UK")
gender <- c("M","F","F","M","F")
age <- c(32,45,25,39,99)
q1 <- c(5,3,3,3,2)
q2 <- c(4,5,5,3,2)
q3 <- c(5,2,5,4,1)
q4 <- c(5,5,5,NA,2)
q5 <- c(5,5,2,NA,1)
leadership <- data.frame(manager,date,country,gender,age,q1,q2,q3,q4,q5,
                         stringsAsFactors = F)
leadership$age[leadership$age == 99] <- NA
leadership <- within(leadership,{
  agecat = NA
  agecat[age > 75]   <- "Elder"
  agecat[age >= 55 & age <= 75] <- "Middle age"
  agecat[age < 55]   <- "Young"
})
leadership

 

 

2、依据第二列筛选2008.10.5到2008.10.25的数据

leadership$date <- as.Date(leadership$date, "%m/%d/%y")
leadership
startday <- as.Date("2008-10-05")
endday <- as.Date("2008-10-25")
new<-leadership[which(leadership$date >= startday & leadership$date <= endday),]
new

 

标签:语言,2008.10,日期,leadership,date,筛选,数据
来源: https://www.cnblogs.com/liujiaxin2018/p/14693269.html