我有这个df,其中有包含日期和时间,日期和时间的列。当然,CH4的观测值和计算出的比率(我有更多,但这与这个问题无关)。
'data.frame': 1420847 obs. of 17 variables
$ Start : Factor w/ 1469 levels "2013-08-31 23:56:09.000",..: 2 2 2 2 2 2 2 2 2 2 ...
$ CO2 : int 1510 1950 1190 1170 780 870 730 740 680 700 ...
$ CH4 : int 66 77 62 58 34 51 36 43 32 40 ...
$ Ratio : num 0.0437 0.0395 0.0521 0.0496 0.0436 ...
$ Start_time: POSIXlt, format: "2013-11-20 00:10:05" "2013-11-20 00:10:05" "2013-11-20 00:10:05" "2013-11-20 00:10:05" ...
$ Start_date: Date, format: "2013-09-01" "2013-09-01" "2013-09-01" "2013-09-01" ...
现在,我希望每天分成4个小时的六个区块,并为每个区块分配编号1-6。但是,问题在于,我只有开始测量的日期和时间(Start_date
和Start_time
(或合并Start
)),因此我认为有必要将每个新值分配Start_time
给一个block
。观测值的长度相差很大,因此无法选择为其分配编号。这是我希望完成的工作:
Start Start_time Start_date CO2 CH4 Ratio block
2013-09-01 00:10:05.000 00:10:05 2013-09-01 1510 66 0.04370861 1
2013-09-01 00:10:05.000 00:10:05 2013-09-01 1950 77 0.03948718 1
2013-09-01 05:16:55.000 05:16:55 2013-09-01 1190 62 0.05210084 2
2013-09-01 05:16:55.000 05:16:55 2013-09-01 1170 58 0.04957265 2
2013-09-01 05:16:55.000 05:16:55 2013-09-01 780 34 0.04358974 2
2013-09-01 12:44:33.000 12:44:33 2013-09-01 870 51 0.05862069 4
2013-09-01 12:44:33.000 12:44:33 2013-09-01 730 36 0.04931507 4
2013-09-01 22:14:23.000 22:14:23 2013-09-01 740 43 0.05810811 6
2013-09-01 22:14:23.000 22:14:23 2013-09-01 680 32 0.04705882 6
2013-09-02 08:37:05.000 08:37:05 2013-09-02 700 40 0.05714286 3
2013-09-02 08:37:05.000 08:37:05 2013-09-02 610 35 0.05737705 3
2013-09-02 17:22:33.000 17:22:33 2013-09-02 630 25 0.03968254 5
2013-09-02 17:22:33.000 17:22:33 2013-09-02 670 40 0.05970149 5
2013-09-02 23:59:44.000 23:59:44 2013-09-02 640 37 0.05781250 6
2013-09-02 23:59:44.000 23:59:44 2013-09-02 730 35 0.04794521 6
我已经搜索了该网站并尝试了Google,但到目前为止,我还没有找到答案。我尝试了以下代码,但在此网站的答案中却找到了,但是没有运气。
qaa <- split(df, cut(strptime(paste(df$Start_date, df$Start_time), format = "%Y-%m-%d %H:%M"),"4 hours"))
以前,我尝试将观察数以分钟为单位,因此我尝试调整该代码。老实说,我不知道我在做什么(您可能会说)。
lst<- split(df, df$Start_date)
nobs <- "4 hours"
List <- unlist(lapply(lst, function(x) {
x$grp <- rep(1:(nrow(x)/nobs+1), each = nobs)[1:nrow(x)]
split(x, x$grp)}), recursive = FALSE)
b <- as.matrix(do.call("rbind", List))
再次提醒您,我是有关R的NOOB,因此花了我很多时间才能解决所有问题。我听不太懂这种语言,但是我正在尽我最大的努力使它生效。我真的很喜欢与它合作!如果此网站上已经存在其他类似问题,请告诉我,以便我删除它。.但是我没有找到它。
感谢您抽出宝贵的时间阅读我的问题并考虑回答该问题!
如果您可以从开始时间中提取开始时间(请尝试在此处:处理R中的时间戳记),则可以使用以下内容分配正确的块号:
df$block[df$start_hour>=0 & df$start_hour<4]<-1
df$block[df$start_hour>=4 & df$start_hour<8]<-2
df$block[df$start_hour>=8 & df$start_hour<12]<-3
df$block[df$start_hour>=12 & df$start_hour<16]<-4
df$block[df$start_hour>=16 & df$start_hour<20]<-5
df$block[df$start_hour>=20 & df$start_hour<24]<-6
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句