简体   繁体   English

使用grep在R中提取和平均每月数据?

[英]Extracting and averaging monthly data in R using grep?

In advance I have to say that I'm truly sorry for poor formatting!事先我不得不说,我真的很抱歉格式不正确! I just can not seem to get a hang of it... I am also an R noob.我似乎无法掌握它......我也是一个 R 菜鸟。

So, I have a 80 files containing information on 80 individual fish's movements;因此,我有 80 个文件,其中包含 80 条鱼的运动信息;

> head(PAVdata)
   X  meanlat meanlong meandepth mindepth maxdepth depthrange rec numhits            datetime mon      X.1       Y        Z
1 23 58.39601 8.708952  6.493529     5.72     7.47       1.75   4      17 2012-05-11 11:15:00   1 482986.7 6472838 6.493529
2 24 58.39618 8.710993  4.035588     1.76     7.47       5.71   6      34 2012-05-11 11:45:00   1 483106.1 6472855 4.035588
3 25 58.39615 8.710919  6.037692     3.08     8.35       5.27   6      26 2012-05-11 12:15:00   1 483101.8 6472853 6.037692
4 26 58.39556 8.710654  3.987500     2.20     6.16       3.96   5      16 2012-05-11 12:45:00   1 483086.0 6472787 3.987500
5 27 58.39617 8.711363  2.944615     2.20     3.96       1.76   6      26 2012-05-11 13:15:00   1 483127.8 6472855 2.944615
6 28 58.39628 8.710904  2.860000     2.20     3.52       1.32   6      24 2012-05-11 13:45:00   1 483101.0 6472867 2.860000

But the columns in interest is datetime and Z , where Z is depth.但感兴趣的列是datetimeZ ,其中 Z 是深度。

What I am aiming to do is;我的目标是;

  • Get a daily and monthly average of depth获取每日和每月平均深度
  • Get a daily and monthly min & max (look at amplitude)获取每日和每月的最小值和最大值(查看幅度)

What I've tried so far is;到目前为止我尝试过的是;

june<-c(PAVdata$datetime[grep("^2012-07",as.character(PAVdata$datetime))])六月<-c(PAVdata$datetime[grep("^2012-07",as.character(PAVdata$datetime))])

Which yield something thats completely farfetched to me这产生了对我来说完全牵强的东西

head(june) [1] 2390 2391 2392 2393 2394 2395负责人(六月) [1] 2390 2391 2392 2393 2394 2395

I was reading about subset and aggregate and POSIXlt and tapply and zoo and wanted to try these, but I seemed to get the error that there was an length diff ..我正在阅读有关子集聚合以及POSIXlttapplyzoo 的内容,并想尝试这些,但我似乎得到了一个错误,即存在长度差异 ..

Heres an attempt with the POSIXlt;这是 POSIXlt 的尝试;

PAVdata$datetime <- strftime(as.POSIXlt(PAVdata$datetime,format="%Y-%m-%d %H:%M:%S"),format='%b') PAVdata$datetime <- strftime(as.POSIXlt(PAVdata$datetime,format="%Y-%m-%d %H:%M:%S"),format='%b')

june_Monthly_Avg <- ave(PAVdata$Z,PAVdata$datetime,FUN=mean) june_Monthly_Avg <- ave(PAVdata$Z,PAVdata$datetime,FUN=mean)

head(june_Monthly_Avg) [1] 9.341862 9.341862 9.341862 9.341862 9.341862 9.341862头(june_Monthly_Avg)[1] 9.341862 9.341862 9.341862 9.341862 9.341862 9.341862

And the whole thing consisted of the same numbers... Think I'm most def using the commando very wrong.整个事情由相同的数字组成......认为我最清楚地使用突击队非常错误。

But then I tried something simpler;但后来我尝试了一些更简单的方法;

juni_Monthly_Avg <- ave(PAVdata$Z,PAVdata$datetime,FUN=mean) juni_Monthly_Avg <- ave(PAVdata$Z,PAVdata$datetime,FUN=mean)

head(juni_Monthly_Avg)头(juni_Monthly_Avg)

[1] 6.493529 4.035588 6.037692 3.987500 2.944615 2.860000 [1] 6.493529 4.035588 6.037692 3.987500 2.944615 2.860000

Confusing to me, seeing that for June I should only get ONE value.让我困惑的是,看到 6 月份我应该只得到一个值。 So far I reckon grep looks most promising, but I can't seem to get my head around it.到目前为止,我认为grep看起来最有希望,但我似乎无法理解它。 Been trawling The R Book and the interwebs and of course SO for a couple days now and decided to cry out for help before I pull out all my hair (and before consulting my supervisors).一直在搜索 R Book 和互联网,当然还有几天,我决定在拔掉所有头发之前(以及在咨询我的主管之前)大声呼救。

Thanks ^_^谢谢^_^

> dput(head(PAVdata))
structure(list(X = 23:28, meanlat = c(58.3960117647059, 58.3961761764706, 
58.3961507692308, 58.395558125, 58.3961726923077, 58.3962804166667
), meanlong = c(8.70895176470588, 8.71099294117647, 8.71091923076923, 
8.71065375, 8.71136346153846, 8.71090416666667), meandepth = c(6.49352941176471, 
4.03558823529412, 6.03769230769231, 3.9875, 2.94461538461538, 
2.86), mindepth = c(5.72, 1.76, 3.08, 2.2, 2.2, 2.2), maxdepth = c(7.47, 
7.47, 8.35, 6.16, 3.96, 3.52), depthrange = c(1.75, 5.71, 5.27, 
3.96, 1.76, 1.32), rec = c(4L, 6L, 6L, 5L, 6L, 6L), numhits = c(17L, 
34L, 26L, 16L, 26L, 24L), datetime = structure(1:6, .Label = c("2012-06-01 00:15:00", "2012-06-01 00:45:00", 
"2012-06-01 01:15:00", "2012-06-01 01:45:00", "2012-06-01 02:15:00", 
"2012-06-01 02:45:00", "2012-06-01 03:15:00", "2012-06-01 03:45:00", 
"2012-06-01 04:15:00", "2012-06-01 04:45:00", "2012-06-01 05:45:00", 
"2012-06-01 06:15:00", "2012-06-01 06:45:00", "2012-06-01 07:15:00", 
"2012-06-01 07:45:00", "2012-06-01 08:15:00", "2012-06-01 08:45:00", 
"2012-06-01 09:45:00", "2012-06-01 10:15:00", "2012-06-01 10:45:00", 
"2012-06-01 11:15:00", "2012-06-01 11:45:00", "2012-06-01 12:15:00", 
"2012-06-01 12:45:00", "2012-06-01 13:15:00", "2012-06-01 13:45:00", 
"2012-06-01 14:45:00", "2012-06-01 15:15:00", "2012-06-01 15:45:00", 
"2012-06-01 16:15:00", "2012-06-01 16:45:00", "2012-06-01 17:45:00", 
"2012-06-01 18:15:00", "2012-06-01 18:45:00", "2012-06-01 19:15:00", 
"2012-06-01 19:45:00", "2012-06-01 20:15:00", "2012-06-01 20:45:00", 
"2012-06-01 21:15:00", "2012-06-01 22:15:00", "2012-06-01 22:45:00", 
"2012-06-01 23:15:00", "2012-06-01 23:45:00", "2012-06-02 00:15:00", 
"2012-06-02 00:45:00", "2012-06-02 01:15:00", "2012-06-02 01:45:00", 
"2012-06-02 02:15:00", "2012-06-02 02:45:00", "2012-06-02 03:15:00", 
"2012-06-02 03:45:00", "2012-06-02 04:15:00", "2012-06-02 04:45:00", 
"2012-06-02 05:15:00", "2012-06-02 05:45:00", "2012-06-02 06:15:00", 
"2012-06-02 06:45:00", "2012-06-02 07:15:00", "2012-06-02 07:45:00", 
"2012-06-02 08:15:00", "2012-06-02 08:45:00", "2012-06-02 09:15:00", 
"2012-06-02 09:45:00", "2012-06-02 10:15:00", "2012-06-02 10:45:00", 
"2012-06-02 11:15:00", "2012-06-02 11:45:00", "2012-06-02 12:15:00", 
"2012-06-02 13:15:00", "2012-06-02 13:45:00", "2012-06-02 14:15:00", 
"2012-06-02 14:45:00", "2012-06-02 15:15:00", "2012-06-02 15:45:00", 
"2012-06-02 16:15:00", "2012-06-02 16:45:00", "2012-06-02 17:15:00", 
"2012-06-02 18:15:00", "2012-06-02 19:15:00", "2012-06-02 19:45:00", 
"2012-06-02 20:15:00", "2012-06-02 20:45:00", "2012-06-02 21:15:00", 
"2012-06-02 21:45:00", "2012-06-02 22:15:00", "2012-06-02 22:45:00", 
"2012-06-02 23:15:00", "2012-06-02 23:45:00", "2012-06-03 00:15:00", 
"2012-06-03 00:45:00", "2012-06-03 01:15:00", "2012-06-03 01:45:00", 
"2012-06-03 02:15:00", "2012-06-03 02:45:00", "2012-06-03 03:15:00", 
"2012-06-03 03:45:00", "2012-06-03 04:15:00", "2012-06-03 04:45:00", 
"2012-06-03 05:15:00", "2012-06-03 05:45:00", "2012-06-03 06:15:00", 
"2012-06-03 06:45:00", "2012-06-03 07:15:00", "2012-06-03 07:45:00", 
"2012-06-03 08:15:00", "2012-06-03 08:45:00", "2012-06-03 09:15:00", 
"2012-06-03 09:45:00", "2012-06-03 10:15:00", "2012-06-03 10:45:00", 
"2012-06-03 11:15:00", "2012-06-03 11:45:00", "2012-06-03 12:15:00", 
"2012-06-03 12:45:00", "2012-06-03 13:15:00", "2012-06-03 13:45:00", 
"2012-06-03 14:15:00", "2012-06-03 15:45:00", "2012-06-03 16:15:00", 
"2012-06-03 16:45:00", "2012-06-03 17:15:00", "2012-06-03 17:45:00", 
"2012-06-03 18:15:00", "2012-06-03 18:45:00", "2012-06-03 19:15:00", 
"2012-06-03 19:45:00", "2012-06-03 20:15:00", "2012-06-03 20:45:00", 
"2012-06-03 21:15:00", "2012-06-03 21:45:00", "2012-06-03 22:15:00", 
"2012-06-03 22:45:00", "2012-06-03 23:15:00", "2012-06-03 23:45:00", 
"2012-06-04 00:15:00", "2012-06-04 00:45:00", "2012-06-04 01:15:00", 
"2012-06-04 01:45:00", "2012-06-04 02:15:00", "2012-06-04 02:45:00", 
"2012-06-04 03:15:00", "2012-06-04 03:45:00", "2012-06-04 04:45:00", 
"2012-06-04 05:15:00", "2012-06-04 05:45:00", "2012-06-04 06:15:00", 
"2012-06-04 06:45:00", "2012-06-04 07:15:00", "2012-06-04 07:45:00", 
"2012-06-04 08:15:00", "2012-06-04 08:45:00", "2012-06-04 09:15:00", 
"2012-06-04 09:45:00", "2012-06-04 10:15:00", "2012-06-04 10:45:00", 
"2012-06-04 11:15:00", "2012-06-04 11:45:00", "2012-06-04 12:15:00", 
"2012-06-04 12:45:00", "2012-06-04 13:15:00", "2012-06-04 13:45:00", 
"2012-06-04 14:15:00", "2012-06-04 14:45:00", "2012-06-04 15:15:00", 
"2012-06-04 15:45:00", "2012-06-04 16:15:00", "2012-06-04 16:45:00", 
"2012-06-04 17:15:00", "2012-06-04 17:45:00", "2012-06-04 18:15:00", 
"2012-06-04 18:45:00", "2012-06-04 19:15:00", "2012-06-04 19:45:00", 
"2012-06-04 20:15:00", "2012-06-04 20:45:00", "2012-06-04 21:15:00", 
"2012-06-04 21:45:00", "2012-06-04 22:15:00", "2012-06-04 22:45:00", 
"2012-06-04 23:15:00", "2012-06-04 23:45:00", "2012-06-05 00:15:00", 
"2012-06-05 00:45:00", "2012-06-05 01:15:00", "2012-06-05 01:45:00", 
"2012-06-05 02:15:00", "2012-06-05 02:45:00", "2012-06-05 03:15:00", 
"2012-06-05 03:45:00", "2012-06-05 04:15:00", "2012-06-05 04:45:00", 
"2012-06-05 05:15:00", "2012-06-05 05:45:00", "2012-06-05 06:15:00", 
"2012-06-05 06:45:00", "2012-06-05 07:15:00", "2012-06-05 07:45:00", 
"2012-06-05 08:15:00", "2012-06-05 08:45:00", "2012-06-05 09:15:00", 
"2012-06-05 09:45:00", "2012-06-05 10:15:00", "2012-06-05 10:45:00", 
"2012-06-05 11:15:00", "2012-06-05 11:45:00", "2012-06-05 12:15:00", 
"2012-06-05 12:45:00", "2012-06-05 13:15:00", "2012-06-05 13:45:00", 
"2012-06-05 14:15:00", "2012-06-05 14:45:00", "2012-06-05 15:15:00", 
"2012-06-05 15:45:00", "2012-06-05 16:15:00", "2012-06-05 16:45:00", 
"2012-06-05 17:15:00", "2012-06-05 17:45:00", "2012-06-05 18:15:00", 
"2012-06-05 18:45:00", "2012-06-05 19:15:00", "2012-06-05 19:45:00", 
"2012-06-05 20:15:00", "2012-06-05 20:45:00", "2012-06-05 21:15:00", 
"2012-06-05 21:45:00", "2012-06-05 22:15:00", "2012-06-05 22:45:00", 
"2012-06-05 23:15:00", "2012-06-05 23:45:00", "2012-06-06 00:15:00", 
"2012-06-06 00:45:00", "2012-06-06 01:15:00", "2012-06-06 01:45:00", 
"2012-06-06 02:15:00", "2012-06-06 02:45:00", "2012-06-06 03:15:00", 
"2012-06-06 03:45:00", "2012-06-06 04:15:00", "2012-06-06 04:45:00", 
"2012-06-06 05:15:00", "2012-06-06 05:45:00", "2012-06-06 06:45:00", 
"2012-06-06 07:15:00", "2012-06-06 07:45:00", "2012-06-06 08:15:00", 
"2012-06-06 08:45:00", "2012-06-06 09:45:00", "2012-06-06 10:15:00", 
"2012-06-06 10:45:00", "2012-06-06 11:15:00", "2012-06-06 11:45:00", 
"2012-06-06 12:15:00", "2012-06-06 12:45:00", "2012-06-06 13:15:00", 
"2012-06-06 13:45:00", "2012-06-06 14:15:00", "2012-06-06 14:45:00", 
"2012-06-06 15:15:00", "2012-06-06 15:45:00", "2012-06-06 16:15:00", 
"2012-06-06 16:45:00", "2012-06-06 17:15:00", "2012-06-06 17:45:00", 
"2012-06-06 18:15:00", "2012-06-06 18:45:00", "2012-06-06 19:15:00", 
"2012-06-06 19:45:00", "2012-06-06 20:15:00", "2012-06-06 20:45:00", 
"2012-06-06 21:45:00", "2012-06-06 22:15:00", "2012-06-06 22:45:00", 
"2012-06-06 23:15:00", "2012-06-06 23:45:00", "2012-06-07 00:15:00", 
"2012-06-07 00:45:00", "2012-06-07 01:15:00", "2012-06-07 01:45:00", 
"2012-06-07 02:15:00", "2012-06-07 02:45:00", "2012-06-07 03:15:00", 
"2012-06-07 03:45:00", "2012-06-07 04:15:00", "2012-06-07 04:45:00", 
"2012-06-07 05:15:00", "2012-06-07 05:45:00", "2012-06-07 06:15:00", 
"2012-06-07 06:45:00", "2012-06-07 07:15:00", "2012-06-07 07:45:00", 
"2012-06-07 08:15:00", "2012-06-07 08:45:00", "2012-06-07 09:15:00", 
"2012-06-07 09:45:00", "2012-06-07 10:15:00", "2012-06-07 10:45:00", 
"2012-06-07 11:15:00", "2012-06-07 11:45:00", "2012-06-07 12:15:00", 
"2012-06-07 12:45:00", "2012-06-07 13:15:00", "2012-06-07 13:45:00", 
"2012-06-07 14:15:00", "2012-06-07 14:45:00", "2012-06-07 15:15:00", 
"2012-06-07 15:45:00", "2012-06-07 16:15:00", "2012-06-07 16:45:00", 
"2012-06-07 17:15:00", "2012-06-07 17:45:00", "2012-06-07 18:15:00", 
"2012-06-07 18:45:00", "2012-06-07 19:15:00", "2012-06-07 19:45:00", 
"2012-06-07 20:15:00", "2012-06-07 20:45:00", "2012-06-07 21:15:00", 
"2012-06-07 21:45:00", "2012-06-07 22:15:00", "2012-06-07 22:45:00", 
"2012-06-07 23:15:00", "2012-06-07 23:45:00", "2012-06-08 00:15:00", 
"2012-06-08 00:45:00", "2012-06-08 01:15:00", "2012-06-08 01:45:00", 
"2012-06-08 02:15:00", "2012-06-08 02:45:00", "2012-06-08 03:15:00", 
"2012-06-08 03:45:00", "2012-06-08 04:15:00", "2012-06-08 04:45:00", 
"2012-06-08 05:15:00", "2012-06-08 05:45:00", "2012-06-08 06:15:00", 
"2012-06-08 06:45:00", "2012-06-08 07:15:00", "2012-06-08 07:45:00", 
"2012-06-08 08:15:00", "2012-06-08 08:45:00", "2012-06-08 09:15:00", 
"2012-06-08 09:45:00", "2012-06-08 10:15:00", "2012-06-08 10:45:00", 
"2012-06-08 11:15:00", "2012-06-08 11:45:00", "2012-06-08 12:15:00", 
"2012-06-08 12:45:00", "2012-06-08 13:15:00", "2012-06-08 13:45:00", 
"2012-06-08 14:15:00", "2012-06-08 14:45:00", "2012-06-08 15:15:00", 
"2012-06-08 15:45:00", "2012-06-08 16:15:00", "2012-06-08 16:45:00", 
"2012-06-08 17:15:00", "2012-06-08 17:45:00", "2012-06-08 18:15:00", 
"2012-06-08 18:45:00", "2012-06-08 19:15:00", "2012-06-08 19:45:00", 
"2012-06-08 20:15:00", "2012-06-08 20:45:00", "2012-06-08 21:15:00", 
"2012-06-08 21:45:00", "2012-06-08 22:15:00", "2012-06-08 22:45:00", 
"2012-06-08 23:15:00", "2012-06-08 23:45:00", "2012-06-09 00:15:00", 
"2012-06-09 00:45:00", "2012-06-09 01:15:00", "2012-06-09 01:45:00", 
"2012-06-09 02:15:00", "2012-06-09 02:45:00", "2012-06-09 03:15:00", 
"2012-06-09 03:45:00", "2012-06-09 04:15:00", "2012-06-09 04:45:00", 
"2012-06-09 05:15:00", "2012-06-09 05:45:00", "2012-06-09 06:45:00", 
"2012-06-09 07:15:00", "2012-06-09 07:45:00", "2012-06-09 08:15:00", 
"2012-06-09 08:45:00", "2012-06-09 09:15:00", "2012-06-09 09:45:00", 
"2012-06-09 10:15:00", "2012-06-09 10:45:00", "2012-06-09 11:15:00", 
"2012-06-09 11:45:00", "2012-06-09 12:15:00", "2012-06-09 12:45:00", 
"2012-06-09 13:15:00", "2012-06-09 13:45:00", "2012-06-09 14:15:00", 
"2012-06-09 14:45:00", "2012-06-09 15:15:00", "2012-06-09 15:45:00", 
"2012-06-09 16:15:00", "2012-06-09 16:45:00", "2012-06-09 17:15:00", 
"2012-06-09 17:45:00", "2012-06-09 18:15:00", "2012-06-09 18:45:00", 
"2012-06-09 19:15:00", "2012-06-09 19:45:00", "2012-06-09 20:15:00", 
"2012-06-09 20:45:00", "2012-06-09 21:15:00", "2012-06-09 21:45:00", 
"2012-06-09 22:15:00", "2012-06-09 22:45:00", "2012-06-09 23:15:00", 
"2012-06-09 23:45:00", "2012-06-10 00:15:00", "2012-06-10 00:45:00", 
"2012-06-10 01:15:00", "2012-06-10 01:45:00", "2012-06-10 02:15:00", 
"2012-06-10 02:45:00", "2012-06-10 03:15:00", "2012-06-10 03:45:00", 
"2012-06-10 04:15:00", "2012-06-10 04:45:00", "2012-06-10 05:15:00", 
"2012-06-10 05:45:00", "2012-06-10 06:15:00", "2012-06-10 06:45:00", 
"2012-06-10 07:15:00", "2012-06-10 07:45:00", "2012-06-10 08:15:00", 
"2012-06-10 08:45:00", "2012-06-10 09:15:00", "2012-06-10 09:45:00", 
"2012-06-10 10:15:00", "2012-06-10 10:45:00", "2012-06-10 11:15:00", 
"2012-06-10 11:45:00", "2012-06-10 12:15:00", "2012-06-10 12:45:00", 
"2012-06-10 13:15:00", "2012-06-10 13:45:00", "2012-06-10 14:15:00", 
"2012-06-10 14:45:00", "2012-06-10 15:15:00", "2012-06-10 15:45:00", 
"2012-06-10 16:15:00", "2012-06-10 16:45:00", "2012-06-10 17:15:00", 
"2012-06-10 17:45:00", "2012-06-10 18:15:00", "2012-06-10 18:45:00", 
"2012-06-10 19:15:00", "2012-06-10 19:45:00", "2012-06-10 20:15:00", 
"2012-06-10 20:45:00", "2012-06-10 21:15:00", "2012-06-10 21:45:00", 
"2012-06-10 22:15:00", "2012-06-10 22:45:00", "2012-06-10 23:15:00", 
"2012-06-10 23:45:00", 
"2012-07-01 00:15:00", "2012-07-01 00:45:00", "2012-07-01 01:15:00", 
"2012-07-01 01:45:00", "2012-07-01 02:15:00", "2012-07-01 02:45:00", 
"2012-07-01 03:15:00", "2012-07-01 03:45:00", "2012-07-01 04:15:00", 
"2012-07-01 04:45:00", "2012-07-01 05:15:00", "2012-07-01 05:45:00", 
"2012-07-01 06:15:00", "2012-07-01 06:45:00", "2012-07-01 07:15:00", 
"2012-07-01 07:45:00", "2012-07-01 08:15:00", "2012-07-01 08:45:00", 
"2012-07-01 09:15:00", "2012-07-01 09:45:00", "2012-07-01 10:15:00", 
"2012-07-01 10:45:00", "2012-07-01 11:15:00", "2012-07-01 11:45:00", 
"2012-07-01 12:15:00", "2012-07-01 12:45:00", "2012-07-01 13:15:00", 
"2012-07-01 13:45:00", "2012-07-01 14:15:00", "2012-07-01 14:45:00", 
"2012-07-01 15:15:00", "2012-07-01 15:45:00", "2012-07-01 16:15:00", 
"2012-07-01 16:45:00", "2012-07-01 17:15:00", "2012-07-01 17:45:00", 
"2012-07-01 18:15:00", "2012-07-01 18:45:00", "2012-07-01 19:15:00", 
"2012-07-01 19:45:00", "2012-07-01 20:15:00", "2012-07-01 20:45:00", 
"2012-07-01 21:15:00", "2012-07-01 21:45:00", "2012-07-01 22:15:00", 
"2012-07-01 22:45:00", "2012-07-01 23:15:00", "2012-07-01 23:45:00", 
"2012-07-02 00:15:00", "2012-07-02 00:45:00", "2012-07-02 01:15:00", 
"2012-07-02 01:45:00", "2012-07-02 02:15:00", "2012-07-02 02:45:00", 
"2012-07-02 03:15:00", "2012-07-02 03:45:00", "2012-07-02 04:15:00", 
"2012-07-02 04:45:00", "2012-07-02 05:15:00", "2012-07-02 05:45:00", 
"2012-07-02 06:15:00", "2012-07-02 06:45:00", "2012-07-02 07:15:00", 
"2012-07-02 07:45:00", "2012-07-02 08:15:00", "2012-07-02 08:45:00", 
"2012-07-02 09:15:00", "2012-07-02 09:45:00", "2012-07-02 10:15:00", 
"2012-07-02 10:45:00", "2012-07-02 11:15:00", "2012-07-02 11:45:00", 
"2012-07-02 12:15:00", "2012-07-02 12:45:00", "2012-07-02 13:15:00", 
"2012-07-02 13:45:00", "2012-07-02 14:15:00", "2012-07-02 14:45:00", 
"2012-07-02 15:15:00", "2012-07-02 15:45:00", "2012-07-02 16:15:00", 
"2012-07-02 16:45:00", "2012-07-02 17:15:00", "2012-07-02 17:45:00", 
"2012-07-02 18:15:00", "2012-07-02 18:45:00", "2012-07-02 19:15:00", 
"2012-07-02 19:45:00", "2012-07-02 20:15:00", "2012-07-02 20:45:00", 
"2012-07-02 21:15:00", "2012-07-02 21:45:00", "2012-07-02 22:15:00", 
"2012-07-02 22:45:00", "2012-07-02 23:15:00", "2012-07-02 23:45:00", 
"2012-07-03 00:15:00", "2012-07-03 00:45:00", "2012-07-03 01:15:00", 
"2012-07-03 01:45:00", "2012-07-03 02:15:00", "2012-07-03 02:45:00", 
"2012-07-03 03:15:00", "2012-07-03 03:45:00", "2012-07-03 04:15:00", 
"2012-07-03 04:45:00", "2012-07-03 05:15:00", "2012-07-03 05:45:00", 
"2012-07-03 06:15:00", "2012-07-03 06:45:00", "2012-07-03 07:15:00", 
"2012-07-03 07:45:00", "2012-07-03 08:15:00", "2012-07-03 08:45:00", 
"2012-07-03 09:15:00", "2012-07-03 09:45:00", "2012-07-03 10:15:00", 
"2012-07-03 10:45:00", "2012-07-03 11:15:00", "2012-07-03 11:45:00", 
"2012-07-03 12:15:00", "2012-07-03 12:45:00", "2012-07-03 13:15:00", 
"2012-07-03 13:45:00", "2012-07-03 14:15:00", "2012-07-03 15:15:00", 
"2012-07-03 15:45:00", "2012-07-03 16:15:00", "2012-07-03 16:45:00", 
"2012-07-03 17:15:00", "2012-07-03 17:45:00", "2012-07-03 18:15:00", 
"2012-07-03 18:45:00", "2012-07-03 19:15:00", "2012-07-03 19:45:00", 
"2012-07-03 20:15:00", "2012-07-03 20:45:00", "2012-07-03 21:15:00", 
"2012-07-03 21:45:00", "2012-07-03 22:15:00", "2012-07-03 22:45:00", 
"2012-07-03 23:15:00", "2012-07-03 23:45:00", "2012-07-04 00:15:00", 
"2012-07-04 00:45:00", "2012-07-04 01:15:00", "2012-07-04 01:45:00", 
"2012-07-04 02:15:00", "2012-07-04 02:45:00", "2012-07-04 03:15:00", 
"2012-07-04 03:45:00", "2012-07-04 04:15:00", "2012-07-04 04:45:00", 
"2012-07-04 05:15:00", "2012-07-04 05:45:00", "2012-07-04 06:15:00", 
"2012-07-04 06:45:00", "2012-07-04 07:15:00", "2012-07-04 07:45:00", 
"2012-07-04 08:45:00", "2012-07-04 09:15:00", "2012-07-04 09:45:00", 
"2012-07-04 10:15:00", "2012-07-04 10:45:00", "2012-07-04 11:15:00", 
"2012-07-04 11:45:00", "2012-07-04 12:15:00", "2012-07-04 12:45:00", 
"2012-07-04 13:15:00", "2012-07-04 13:45:00", "2012-07-04 14:15:00", 
"2012-07-04 14:45:00", "2012-07-04 15:15:00", "2012-07-04 15:45:00", 
"2012-07-04 16:15:00", "2012-07-04 16:45:00", "2012-07-04 17:15:00", 
"2012-07-04 17:45:00", "2012-07-04 18:15:00", "2012-07-04 18:45:00", 
"2012-07-04 19:15:00", "2012-07-04 19:45:00", "2012-07-04 20:15:00", 
"2012-07-04 20:45:00", "2012-07-04 21:15:00", "2012-07-04 21:45:00", 
"2012-07-04 22:15:00", "2012-07-04 22:45:00", "2012-07-04 23:15:00", 
"2012-07-04 23:45:00", "2012-07-05 00:15:00", "2012-07-05 00:45:00", 
"2012-07-05 01:15:00", "2012-07-05 01:45:00", "2012-07-05 02:15:00", 
"2012-07-05 02:45:00", "2012-07-05 03:15:00", "2012-07-05 03:45:00", 
"2012-07-05 04:15:00", "2012-07-05 04:45:00", "2012-07-05 05:15:00", 
"2012-07-05 05:45:00", "2012-07-05 06:15:00", "2012-07-05 06:45:00", 
"2012-07-05 07:15:00", "2012-07-05 07:45:00", "2012-07-05 08:15:00", 
"2012-07-05 08:45:00", "2012-07-05 09:15:00", "2012-07-05 09:45:00", 
"2012-07-05 10:15:00", "2012-07-05 10:45:00", "2012-07-05 11:15:00", 
"2012-07-05 11:45:00", "2012-07-05 12:15:00", "2012-07-05 12:45:00", 
"2012-07-05 13:15:00", "2012-07-05 13:45:00", "2012-07-05 14:15:00", 
"2012-07-05 14:45:00", "2012-07-05 15:15:00", "2012-07-05 15:45:00", 
"2012-07-05 16:45:00", "2012-07-05 17:15:00", "2012-07-05 17:45:00", 
"2012-07-05 18:15:00", "2012-07-05 18:45:00", "2012-07-05 19:15:00", 
"2012-07-05 19:45:00", "2012-07-05 20:15:00", "2012-07-05 20:45:00", 
"2012-07-05 21:15:00", "2012-07-05 21:45:00", "2012-07-05 22:15:00", 
"2012-07-05 22:45:00", "2012-07-05 23:15:00", "2012-07-05 23:45:00", 
"2012-07-06 00:15:00", "2012-07-06 00:45:00", "2012-07-06 01:15:00", 
"2012-07-06 01:45:00", "2012-07-06 02:15:00", "2012-07-06 02:45:00", 
"2012-07-06 03:15:00", "2012-07-06 03:45:00", "2012-07-06 04:15:00", 
"2012-07-06 04:45:00", "2012-07-06 05:15:00", "2012-07-06 05:45:00", 
"2012-07-06 06:15:00", "2012-07-06 06:45:00", "2012-07-06 07:15:00", 
"2012-07-06 07:45:00", "2012-07-06 08:15:00", "2012-07-06 08:45:00", 
"2012-07-06 09:15:00", "2012-07-06 09:45:00", "2012-07-06 10:15:00", 
"2012-07-06 10:45:00", "2012-07-06 11:15:00", "2012-07-06 11:45:00", 
"2012-07-06 12:15:00", "2012-07-06 12:45:00", "2012-07-06 13:15:00", 
"2012-07-06 13:45:00", "2012-07-06 14:15:00", "2012-07-06 14:45:00", 
"2012-07-06 15:15:00", "2012-07-06 15:45:00", "2012-07-06 16:15:00", 
"2012-07-06 16:45:00", "2012-07-06 17:15:00", "2012-07-06 17:45:00", 
"2012-07-06 18:15:00", "2012-07-06 18:45:00", "2012-07-06 19:15:00", 
"2012-07-06 19:45:00", "2012-07-06 20:15:00", "2012-07-06 20:45:00", 
"2012-07-06 21:15:00", "2012-07-06 21:45:00", "2012-07-06 22:15:00", 
"2012-07-06 22:45:00", "2012-07-06 23:15:00", "2012-07-06 23:45:00", 
"2012-07-07 00:15:00", "2012-07-07 00:45:00", "2012-07-07 01:15:00", 
"2012-07-07 01:45:00", "2012-07-07 02:15:00", "2012-07-07 02:45:00", 
"2012-07-07 03:15:00", "2012-07-07 03:45:00", "2012-07-07 04:15:00", 
"2012-07-07 04:45:00", "2012-07-07 05:15:00", "2012-07-07 05:45:00", 
"2012-07-07 06:15:00", "2012-07-07 06:45:00", "2012-07-07 07:15:00", 
"2012-07-07 07:45:00", "2012-07-07 08:15:00", "2012-07-07 08:45:00", 
"2012-07-07 09:15:00", "2012-07-07 09:45:00", "2012-07-07 10:15:00", 
"2012-07-07 10:45:00", "2012-07-07 11:45:00", "2012-07-07 12:15:00", 
"2012-07-07 12:45:00", "2012-07-07 13:15:00", "2012-07-07 13:45:00", 
"2012-07-07 14:15:00", "2012-07-07 14:45:00", "2012-07-07 15:15:00", 
"2012-07-07 15:45:00", "2012-07-07 16:15:00", "2012-07-07 16:45:00", 
"2012-07-07 17:15:00", "2012-07-07 17:45:00", "2012-07-07 18:15:00", 
"2012-07-07 18:45:00", "2012-07-07 19:15:00", "2012-07-07 19:45:00", 
"2012-07-07 20:15:00", "2012-07-07 20:45:00", "2012-07-07 21:15:00", 
"2012-07-07 21:45:00", "2012-07-07 22:15:00", "2012-07-07 22:45:00", 
"2012-07-07 23:15:00", "2012-07-07 23:45:00", "2012-07-08 00:15:00", 
"2012-07-08 00:45:00", "2012-07-08 01:15:00", "2012-07-08 01:45:00", 
"2012-07-08 02:15:00", "2012-07-08 02:45:00", "2012-07-08 03:15:00", 
"2012-07-08 03:45:00", "2012-07-08 04:15:00", "2012-07-08 04:45:00", 
"2012-07-08 05:15:00", "2012-07-08 05:45:00", "2012-07-08 06:15:00", 
"2012-07-08 06:45:00", "2012-07-08 07:45:00", "2012-07-08 08:15:00", 
"2012-07-08 08:45:00", "2012-07-08 09:15:00", "2012-07-08 09:45:00", 
"2012-07-08 10:15:00", "2012-07-08 10:45:00", "2012-07-08 11:15:00", 
"2012-07-08 11:45:00", "2012-07-08 12:15:00", "2012-07-08 12:45:00", 
"2012-07-08 13:15:00", "2012-07-08 13:45:00", "2012-07-08 14:15:00", 
"2012-07-08 14:45:00", "2012-07-08 15:15:00", "2012-07-08 16:15:00", 
"2012-07-08 16:45:00", "2012-07-08 17:15:00", "2012-07-08 17:45:00", 
"2012-07-08 18:15:00", "2012-07-08 18:45:00", "2012-07-08 19:15:00", 
"2012-07-08 19:45:00", "2012-07-08 20:15:00", "2012-07-08 20:45:00", 
"2012-07-08 21:15:00", "2012-07-08 21:45:00", "2012-07-08 22:15:00", 
"2012-07-08 22:45:00", "2012-07-08 23:15:00", "2012-07-08 23:45:00", 
"2012-07-09 00:15:00", "2012-07-09 00:45:00", "2012-07-09 01:15:00", 
"2012-07-09 01:45:00", "2012-07-09 02:15:00", "2012-07-09 02:45:00", 
"2012-07-09 03:15:00", "2012-07-09 03:45:00", "2012-07-09 04:15:00", 
"2012-07-09 04:45:00", "2012-07-09 05:15:00", "2012-07-09 05:45:00", 
"2012-07-09 06:15:00", "2012-07-09 06:45:00", "2012-07-09 07:15:00", 
"2012-07-09 07:45:00", "2012-07-09 08:15:00", "2012-07-09 08:45:00", 
"2012-07-09 09:15:00", "2012-07-09 09:45:00", "2012-07-09 10:15:00", 
"2012-07-09 10:45:00", "2012-07-09 11:15:00", "2012-07-09 11:45:00", 
"2012-07-09 12:15:00", "2012-07-09 12:45:00", "2012-07-09 13:15:00", 
"2012-07-09 13:45:00", "2012-07-09 14:15:00", "2012-07-09 14:45:00", 
"2012-07-09 15:15:00", "2012-07-09 15:45:00", "2012-07-09 16:15:00", 
"2012-07-09 16:45:00", "2012-07-09 17:15:00", "2012-07-09 17:45:00", 
"2012-07-09 18:15:00", "2012-07-09 18:45:00", "2012-07-09 19:15:00", 
"2012-07-09 19:45:00", "2012-07-09 20:15:00", "2012-07-09 21:15:00", 
"2012-07-09 21:45:00", "2012-07-09 22:15:00", "2012-07-09 22:45:00", 
"2012-07-09 23:15:00", "2012-07-09 23:45:00", "2012-07-10 00:15:00", 
"2012-07-10 00:45:00", "2012-07-10 01:15:00", "2012-07-10 01:45:00", 
"2012-07-10 02:15:00", "2012-07-10 02:45:00", "2012-07-10 03:15:00", 
"2012-07-10 03:45:00", "2012-07-10 04:15:00", "2012-07-10 04:45:00", 
"2012-07-10 05:15:00", "2012-07-10 05:45:00", "2012-07-10 06:15:00", 
"2012-07-10 06:45:00", "2012-07-10 07:15:00", "2012-07-10 07:45:00", 
"2012-07-10 08:15:00", "2012-07-10 08:45:00", "2012-07-10 09:15:00", 
"2012-07-10 09:45:00", "2012-07-10 10:15:00", "2012-07-10 10:45:00", 
"2012-07-10 11:15:00", "2012-07-10 11:45:00", "2012-07-10 12:15:00", 
"2012-07-10 12:45:00", "2012-07-10 13:15:00", "2012-07-10 13:45:00", 
"2012-07-10 14:15:00", "2012-07-10 14:45:00", "2012-07-10 15:15:00", 
"2012-07-10 15:45:00", "2012-07-10 16:15:00", "2012-07-10 17:15:00", 
"2012-07-10 17:45:00", "2012-07-10 18:15:00", "2012-07-10 18:45:00", 
"2012-07-10 19:15:00", "2012-07-10 19:45:00", "2012-07-10 20:15:00", 
"2012-07-10 20:45:00", "2012-07-10 21:15:00", "2012-07-10 21:45:00", 
"2012-07-10 22:15:00", "2012-07-10 22:45:00", "2012-07-10 23:15:00", 
"2012-07-10 23:45:00"), class = "factor"), mon = c(1L, 1L, 1L, 
1L, 1L, 1L), X.1 = c(482986.747312444, 483106.142664631, 483101.821796104, 
483086.019405256, 483127.799620954, 483101.003261731), Y = c(6472837.55691427, 
6472855.34802117, 6472852.53770682, 6472786.61976395, 6472854.86710904, 
6472866.97636095), Z = c(6.49352941176471, 4.03558823529412, 
6.03769230769231, 3.9875, 2.94461538461538, 2.86)), .Names = c("X", 
"meanlat", "meanlong", "meandepth", "mindepth", "maxdepth", "depthrange", 
"rec", "numhits", "datetime", "mon", "X.1", "Y", "Z"), row.names = c(NA, 
6L), class = "data.frame")
> 

You look that You tray many things but you don't try one thing in a good way.你看起来你托盘很多东西,但你没有以好的方式尝试一件事。

For example using POSIXlt , dat here is your PAVdata object例如使用POSIXlt ,这里的 dat 是您的 PAVdata 对象

ll <- as.POSIXlt(dat$datetime)
dat$hours <- ll$hour              ## I create a variable hour
dat$days <- ll$mday               ## I create a variable day
dat$months <- ll$mon              ## I create a variable month

Now i can use this variable to average by group.现在我可以使用这个变量按组平均。

here a solution using plyr package.这里使用plyr包的解决方案。

 ddply(dat,.(months,days),summarise, m =  mean(meandepth))    ## I group by months and days
  months days        m
1      4   11 4.393154
> ddply(dat,.(months,days,hours),summarise, m =  mean(meandepth)) 
  months days hours        m
1      4   11    11 5.264558
2      4   11    12 5.012596
3      4   11    13 2.902308

Whatever you use, try to use one thing in a good way.无论您使用什么,请尝试以好的方式使用一件事。 Generally there are many method to do such things in R.通常在 R 中有很多方法可以做这样的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM