R中多个日期之间的子集

时间:2016-12-15 06:50:18

标签: r date subset

我有以下数据:

SN,CY,Y,M,D,lat,lon,cat
198305,5,1983,8,5,0,9.1,140.7,"TD"
198305,5,1983,8,5,6,9.3,140.5,"TD"
198305,5,1983,8,5,12,9.6,139.9,"TD"
198305,5,1983,8,5,18,9.9,139.4,"TS"
198305,5,1983,8,6,0,10.2,138.8,"TS"
198305,5,1983,8,6,6,11,138.1,"TS"
198305,5,1983,8,6,12,11.8,137.3,"TS"
198305,5,1983,8,6,18,12.4,136.4,"Cat1"
198305,5,1983,8,7,0,12.8,135.8,"Cat1"
198305,5,1983,8,7,6,13.6,134.7,"Cat1"
198305,5,1983,8,7,12,14.4,133.9,"Cat2"
198305,5,1983,8,7,18,15,133.5,"Cat4"
198305,5,1983,8,8,0,15.8,132.8,"Cat4"
198305,5,1983,8,8,6,16.3,132.4,"Cat4"
198305,5,1983,8,8,12,17.1,132,"Cat5"
198305,5,1983,8,8,18,17.4,131.4,"Cat5"
198305,5,1983,8,9,0,17.8,130.8,"Cat5"
198305,5,1983,8,9,6,18.1,130.7,"Cat4"
198305,5,1983,8,9,12,18.7,130.3,"Cat4"
198305,5,1983,8,9,18,18.9,130.4,"Cat4"
198305,5,1983,8,10,0,19.2,130.4,"Cat4"
198305,5,1983,8,10,6,19.5,130.4,"Cat4"
198305,5,1983,8,10,12,19.9,130.5,"Cat4"
198305,5,1983,8,10,18,20.1,130.5,"Cat3"
198305,5,1983,8,11,0,20.6,130.7,"Cat3"
198305,5,1983,8,11,6,21.2,130.8,"Cat4"
198305,5,1983,8,11,12,21.6,130.7,"Cat4"
198305,5,1983,8,11,18,22,130.6,"Cat4"
198305,5,1983,8,12,0,22.5,130.7,"Cat4"
198305,5,1983,8,12,6,22.9,130.8,"Cat4"
198305,5,1983,8,12,12,23.4,131.2,"Cat4"
198305,5,1983,8,12,18,23.9,131.5,"Cat4"
198305,5,1983,8,13,0,24.6,131.8,"Cat4"
198305,5,1983,8,13,6,24.9,132.2,"Cat3"
198306,6,1983,8,13,6,19.2,119.4,"TD"
198305,5,1983,8,13,12,25.4,132.6,"Cat3"
198306,6,1983,8,13,12,19.4,120.2,"TD"
198305,5,1983,8,13,18,26.1,133,"Cat3"
198306,6,1983,8,13,18,19.6,120.8,"TD"
198305,5,1983,8,14,0,26.6,133.3,"Cat3"
198306,6,1983,8,14,0,19.8,121.5,"TS"
198305,5,1983,8,14,6,27.2,133.5,"Cat3"
198306,6,1983,8,14,6,19.9,122.7,"TS"
198305,5,1983,8,14,12,27.6,133.5,"Cat3"
198306,6,1983,8,14,12,19.9,124.4,"TS"
198305,5,1983,8,14,18,28.1,133.5,"Cat2"
198306,6,1983,8,14,18,20.2,126.2,"TS"
198305,5,1983,8,15,0,28.5,133.6,"Cat2"
198306,6,1983,8,15,0,21,128.3,"TD"
198305,5,1983,8,15,6,29.1,134.1,"Cat2"
198306,6,1983,8,15,6,22,130,"TD"
198305,5,1983,8,15,12,29.5,134.3,"Cat2"
198305,5,1983,8,15,18,30,134.8,"Cat1"
198305,5,1983,8,16,0,31.1,135.1,"Cat1"
198305,5,1983,8,16,6,31.8,135.8,"Cat1"
198305,5,1983,8,16,12,32.7,136.5,"Cat1"
198305,5,1983,8,16,18,33.8,137,"Cat1"
198305,5,1983,8,17,0,34.6,137.3,"TS"
198305,5,1983,8,17,6,35.1,138.1,"TS"
198308,8,1983,8,17,6,15.2,140.1,"TD"
198305,5,1983,8,17,12,35.6,139.4,"TS"
198308,8,1983,8,17,12,15.4,139,"TD"
198308,8,1983,8,17,18,15.4,138.1,"TD"
198308,8,1983,8,18,0,15.2,137.2,"TD"
198308,8,1983,8,18,6,15,136.3,"TD"
198308,8,1983,8,18,12,15.2,135.3,"TD"
198308,8,1983,8,18,18,15.6,134.5,"TD"
198308,8,1983,8,19,0,15.9,133.8,"TS"
198308,8,1983,8,19,6,16.1,133.2,"TS"
198308,8,1983,8,19,12,16.3,132.8,"TS"
198308,8,1983,8,19,18,16.5,132.4,"TS"
198308,8,1983,8,20,0,16.9,132.2,"TS"
198308,8,1983,8,20,6,17.4,132.5,"TS"
198308,8,1983,8,20,12,18,133,"TS"
198308,8,1983,8,20,18,18.7,133.7,"TS"
198308,8,1983,8,21,0,19.3,134.7,"TS"
198308,8,1983,8,21,6,20,135.6,"TS"
198308,8,1983,8,21,12,20.7,136.3,"TD"
198308,8,1983,8,21,18,21.4,137.1,"TD"
198308,8,1983,8,22,0,22.2,137.9,"TD"
198308,8,1983,8,22,6,23.2,138.7,"TD"
198308,8,1983,8,22,12,23.8,140.1,"TS"
198308,8,1983,8,22,18,24.2,141.6,"TS"
198308,8,1983,8,23,0,24.9,142.6,"TS"
198308,8,1983,8,23,6,25.4,143.1,"TS"
198308,8,1983,8,23,12,25.8,143.5,"TS"
198308,8,1983,8,23,18,25.5,144,"TS"
198308,8,1983,8,24,0,25.1,144.3,"TS"
198308,8,1983,8,24,6,25.3,144.7,"TS"
198308,8,1983,8,24,12,25.8,144.7,"TS"
198308,8,1983,8,24,18,26.2,144.1,"TS"
198308,8,1983,8,25,0,26.5,143.4,"TS"
198308,8,1983,8,25,6,27.1,142.8,"TS"
198309,9,1983,8,25,6,25.5,128.1,"TD"
198308,8,1983,8,25,12,27.9,141.8,"TS"
198309,9,1983,8,25,12,25.6,128.8,"TD"
198308,8,1983,8,25,18,29.1,141.3,"TS"
198309,9,1983,8,25,18,26.6,128.7,"TD"
198308,8,1983,8,26,0,30,141.2,"TD"
198309,9,1983,8,26,0,27.6,129.1,"TD"
198308,8,1983,8,26,6,31.2,141.3,"TD"
198309,9,1983,8,26,6,29,129.2,"TD"
198309,9,1983,8,26,12,30.3,128.9,"TD"
198309,9,1983,8,26,18,31.8,128.4,"TD"
198309,9,1983,8,27,0,33.3,127.4,"TD"
198310,10,1983,8,27,0,9.8,183.2,"TD"
198309,9,1983,8,27,6,34.9,126.9,"TD"
198310,10,1983,8,27,6,10.2,181.7,"TD"
198310,10,1983,8,27,12,10.7,180.2,"TD"
198310,10,1983,8,27,18,11.3,178.8,"TD"
198310,10,1983,8,28,0,11.8,177.6,"TD"
198310,10,1983,8,28,6,12.2,176.1,"TD"
198310,10,1983,8,28,12,12.7,174.7,"TD"
198310,10,1983,8,28,18,13.1,173,"TD"
198310,10,1983,8,29,0,13.1,171.3,"TS"
198310,10,1983,8,29,6,13.2,169.9,"TS"
198310,10,1983,8,29,12,13.2,168.5,"TS"
198310,10,1983,8,29,18,13.1,166.8,"TS"
198310,10,1983,8,30,0,13,165.1,"TS"
198310,10,1983,8,30,6,12.5,163.7,"TS"
198310,10,1983,8,30,12,11.9,162,"TS"
198310,10,1983,8,30,18,11.4,160,"TS"
198310,10,1983,8,31,0,10.9,158.4,"TS"
198310,10,1983,8,31,6,10.5,156.9,"TS"
198310,10,1983,8,31,12,10.3,155,"TS"
198310,10,1983,8,31,18,10.2,153,"TS"

SN是唯一标识符,Y表示年,M - 月,D - 天。 我想基于唯一标识符(SN)以及以下日期之间对这些数据进行子集化:

a). August 1 to August 10
b). August 11 to August 21
c). August 22 to August 30

我尝试了以下命令但手动完成。我希望在R中有一种更简单的方法。

test<-dat1983[which(dat1983$Month == '8' & dat1983$Day >= "10" & dat1983$Day <= "20"),]

如果唯一标识符已于8月1日至8月10日期间计算,则不应再计入8月11日至8月21日。任何人都建议我如何在R中执行此操作?

我将不胜感激。

1 个答案:

答案 0 :(得分:0)

我们可以使用&#39; days&#39;创建两组向量。然后通过比较&#39;向量的相应元素,用Map做子集。获得list数据框架

d1 <- c(1, 11, 22)
d2 <- c(10, 21, 30)
res <- Map(function(x,y) subset(df1, M==8 & D >=x & D <= y), d1, d2)
lapply(res, head, 2)
#[[1]]
#      SN CY    Y M D lat lon   cat ID
#1 198305  5 1983 8 5   0 9.1 140.7 TD
#2 198305  5 1983 8 5   6 9.3 140.5 TD

#[[2]]
#      SN CY    Y M  D lat  lon   cat   ID
#25 198305  5 1983 8 11   0 20.6 130.7 Cat3
#26 198305  5 1983 8 11   6 21.2 130.8 Cat4

#[[3]]
#      SN CY    Y M  D lat  lon   cat ID
#80 198308  8 1983 8 22   0 22.2 137.9 TD
#81 198308  8 1983 8 22   6 23.2 138.7 TD

注意:在数据集中,9列只有8个列名称,因此将最后一列名称添加为&#34; ID&#34;。