使用每分钟数据帧计算12点至1点之间的小时平均值:dplyr

时间:2019-01-14 23:54:17

标签: r dplyr mean lubridate

我每分钟的时间序列已有很多年了。

我需要为每个分钟数据点计算以下值:

q <-(Fn-Fd)/ Fn

因此,Fn是夜间12-1 AM之间的平均F值,而Fd只是分钟数据点。

现在显然Fn每天都在变化,因此一种方法可能是使用dplyr函数计算Fn,而我需要创建某种循环或重新组织我的数据框...

虚拟数据:

#string of dates for a one month
datetime <- seq(
     from=as.POSIXct("2012-1-1 0:00:00", tz="UTC"),
     to=as.POSIXct("2012-2-1 0:00:00", tz="UTC"),
     by="min"
) 

#variable F
F <- runif(44641, min = 0, max =2)

#dataframe
df <- as.data.frame(cbind(datetime,F))
library(lubridate)
#make sure its in  "POSIXct" "POSIXt" format
df$datetime <- as_datetime(df$datetime)

或者不太优雅的方法可能是在第一次使用dplyr的时间之间自行获得Fn-我认为它将是这样的:

Fn <- df %>% 
  filter(between(as.numeric(format(datetime, "%H")), 0, 1)) %>% 
  group_by(hour=format(datetime, "%Y-%m-%d %H:")) %>%
  summarise(value=mean(df$F))

但是我不确定我的语法在那里正确吗?我是否在计算每天12点到1点之间的平均F?

然后我可以将每天每分钟的平均Fn值打印到我的数据框中,并进行简单的计算以获得Q。

在此先感谢您的建议。

1 个答案:

答案 0 :(得分:1)

也许是这样吗?

group_by

我们首先每个日期mean每天获取第0小时的value值(00:00到00:59之间的值),并使用给定的公式计算-(IBAction)imagepickertapped:(id)sender { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Attach image" message:@"" preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction* pickFromGallery = [UIAlertAction actionWithTitle:@"Take a photo" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { UIImagePickerController* picker = [[UIImagePickerController alloc] init]; picker.sourceType = UIImagePickerControllerSourceTypeCamera; picker.delegate = self; [self presentViewController:picker animated:YES completion:NULL]; } }]; UIAlertAction* takeAPicture = [UIAlertAction actionWithTitle:@"Choose from gallery" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { UIImagePickerController* picker = [[UIImagePickerController alloc] init]; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; picker.delegate = self; [self presentViewController:picker animated:YES completion:NULL]; }]; UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) { }]; [alertController addAction:pickFromGallery]; [alertController addAction:takeAPicture]; [alertController addAction:cancel]; [self presentViewController:alertController animated:YES completion:nil]; } -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info { UIImage *LoveImage = info[UIImagePickerControllerOriginalImage]; [picker dismissViewControllerAnimated:YES completion:nil]; self.LoveImage = info[UIImagePickerControllerOriginalImage];} -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [picker dismissViewControllerAnimated:YES completion:nil];}