如果在日期范围内,则指定值

时间:2014-12-14 18:06:48

标签: r if-statement

我有一堆大学数据和日期。我想在我的数据框架中创建一个变量,按照学期("秋天","春""夏天")进行编码,查看日期范围一年,所以我想编写一系列if if语句(通过参考历史大学日历)。我正在努力解决如何做到这一点。我想它看起来会像这样:

if( ( (mydata$date>="2009-08-24") and (mydata$date<="2009-12-18") ) == TRUE),
   mydata$semester <- "Fall"

我有很多这样的陈述

简而言之:if(condition and condition)= true,然后应用值

似乎很简单!

在它变得清晰之后,我没有足够好地解释自己(当天我发布这个问题的同一天),下面创建了样本数据:

# Assemble Data  (real dataset is much larger, 70,000 records)
Score<-c(23,2,15,2,12,44,2,NA,34,64,2,34,32,54,NA,17,37,33,32,4,13,19,23,34,86)  
Date<-c('2012-08-27','2012-09-03','2012-09-04','2012-09-28','2012-10-19','2012-10-19','2012-10-19','2012-11-06','2012-11-20','2012-12-13','2013-01-16','2013-01-30','2013-01-30','2013-01-30','2013-02-07','2013-02-14','2013-02-21','2013-02-22','2013-03-03','2013-03-04','2013-03-20','2013-04-02','2013-04-11','2013-04-27','2013-05-04')

#create data frame
university<-data.frame(Date,Score)

# code below doesn't work, because once I have correct value for unversity$Semester I don't want to replace it with "NA" or "Not Fall"
ifelse(university$Date >= '2012-08-27' & university$Date <= '2012-09-02', university$Semester<-"Fall", do-not-overwirte-correct-assignment-so-do-nothing)
ifelse(university$Date >= '2012-09-03' & university$Date <= '2012-09-03', university$Semester<-"Labor Day", do-not-overwirte-correct-assignment-so-do-nothing)
ifelse(university$Date >= '2012-09-04' & university$Date <= '2012-11-16', university$Semester<-"Fall", do-not-overwirte-correct-assignment-so-do-nothing)
ifelse(university$Date >= '2012-11-17' & university$Date <= '2012-11-25', university$Semester<-"Fall Break", do-not-overwirte-correct-assignment-so-do-nothing)
ifelse(university$Date >= '2012-11-26' & university$Date <= '2012-12-13', university$Semester<-"Fall", do-not-overwirte-correct-assignment-so-do-nothing)
ifelse(university$Date >= '2012-12-14' & university$Date <= '2012-12-20', university$Semester<-"Fall Finals", do-not-overwirte-correct-assignment-so-do-nothing)
ifelse(university$Date >= '2012-12-21' & university$Date <= '2013-01-13', university$Semester<-"Winter Break", do-not-overwirte-correct-assignment-so-do-nothing)
ifelse(university$Date >= '2013-01-14' & university$Date <= '2013-01-20', university$Semester<-"Spring", do-not-overwirte-correct-assignment-so-do-nothing)
ifelse(university$Date >= '2013-01-21' & university$Date <= '2013-01-21', university$Semester<-"MLK Day", do-not-overwirte-correct-assignment-so-do-nothing)
ifelse(university$Date >= '2013-01-22' & university$Date <= '2013-03-15', university$Semester<-"Spring", do-not-overwirte-correct-assignment-so-do-nothing)
ifelse(university$Date >= '2013-03-16' & university$Date <= '2013-03-24', university$Semester<-"Spring Break", do-not-overwirte-correct-assignment-so-do-nothing)
ifelse(university$Date >= '2013-03-25' & university$Date <= '2013-05-02', university$Semester<-"Spring", do-not-overwirte-correct-assignment-so-do-nothing)
ifelse(university$Date >= '2013-05-03' & university$Date <= '2013-05-10', university$Semester<-"Spring Finals", do-not-overwirte-correct-assignment-so-do-nothing)

如果上述ifelse语句有效,数据框架大学将如下所示:

日期分数学期
2012-08-27 23秋季
2012-09-03 2劳动节
2012-09-04 15秋季
2012-09-28 2秋季
2012-10-19 12秋季 2012-10-19 44秋季 2012-10-19 2秋季 2012-11-06 NA秋季
2012-11-20 34秋季休息 2012-12-13 64秋季
2013-01-16 2春天
2013-01-30 34春天 2013-01-30 32春天
2013-01-30 54春天 2013-02-07 NA春天 2013-02-14 17春天
2013-02-21 37春天 2013-02-22 33春天 2013-03-03 32春天 2013-03-04 4春天
2013-03-20 13春假 2013-04-02 19春天
2013-04-11 23春天
2013-04-27 34春天 2013-05-04 86春季总决赛

0 个答案:

没有答案