基于每日最大值来设置数据帧 [英] Subsetting a dataframe based on daily maxima

查看:95
本文介绍了基于每日最大值来设置数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个excel csv与日期/时间列和与该日期/时间相关联的值。我正在尝试编写一个脚本,通过这种格式(见下文),并找到1)每天的最大值,以及2)最大值发生的那一天的时间。 R最好在新的数据框中返回两个值。



数据看起来像这样:

  V1 V2 V3 
1 5/1/2012 3:00 1
2 5/1/2012 6:00 2
3 5/1/2012 9:00 5
4 5/1/2012 12:00 3
5 5/1/2012 15:00 6
6 5/1/2012 18:00 2
7 5/1/2012 21:00 1
8 5/2/2012 0:00 2
9 5/2/2012 3:00 3
10 5/2/2012 6: 00 6
11 5/2/2012 9:00 4
12 5/2/2012 12:00 6
13 5/2/2012 15:00 7
14 5 / 2/2012 18:00 9
15 5/2/2012 21:00 1

所以我设想的功能将返回:

  1 5/1/2012 15:00 6 
2 5/2/2012 18:00 9

任何想法?

解决方案

这里你去:

  dat.str < 'V1 V2 V3 
1 5/1/2012 3:00 1
2 5/1/2012 6:00 2
3 5/1/2012 9:00 5
4 5/1/2012 12:00 3
5 5/1 / 2012 15:00 6
6 5/1/2012 18:00 2
7 5/1/2012 21:00 1
8 5/2/2012 0:00 2
9 5/2/2012 3:00 3
10 5/2/2012 6:00 6
11 5/2/2012 9:00 4
12 5/2/2012 12 :00 6
13 5/2/2012 15:00 7
14 5/2/2012 18:00 9
15 5/2/2012 21:00 1'

dat< - read.table(textConnection(dat.str),row.names = 1,header = TRUE)

do.call(rbind,
by(dat ,INDICES = dat $ V1,FUN = function(x)tail(x [order(x $ V3),],1)))


I have an excel csv with a date/time column and a value associated with that date/time. I'm trying to write a script that will go through this format (see below), and find 1) the maximum value per day, and 2) the time on that day that the maximum occurs. Preferably R would return both values to me in a new dataframe.

The data looks something like this:

         V1    V2 V3
1  5/1/2012  3:00  1
2  5/1/2012  6:00  2
3  5/1/2012  9:00  5
4  5/1/2012 12:00  3
5  5/1/2012 15:00  6
6  5/1/2012 18:00  2
7  5/1/2012 21:00  1
8  5/2/2012  0:00  2
9  5/2/2012  3:00  3
10 5/2/2012  6:00  6
11 5/2/2012  9:00  4
12 5/2/2012 12:00  6
13 5/2/2012 15:00  7
14 5/2/2012 18:00  9
15 5/2/2012 21:00  1

So the function I'm envisioning would return:

1 5/1/2012 15:00 6
2 5/2/2012 18:00 9

Any ideas?

解决方案

here you go:

dat.str <- '         V1    V2 V3
1  5/1/2012  3:00  1
2  5/1/2012  6:00  2
3  5/1/2012  9:00  5
4  5/1/2012 12:00  3
5  5/1/2012 15:00  6
6  5/1/2012 18:00  2
7  5/1/2012 21:00  1
8  5/2/2012  0:00  2
9  5/2/2012  3:00  3
10 5/2/2012  6:00  6
11 5/2/2012  9:00  4
12 5/2/2012 12:00  6
13 5/2/2012 15:00  7
14 5/2/2012 18:00  9
15 5/2/2012 21:00  1'

dat <- read.table(textConnection(dat.str), row.names=1, header=TRUE)

do.call(rbind, 
        by(dat, INDICES=dat$V1, FUN=function(x) tail(x[order(x$V3), ], 1)))

这篇关于基于每日最大值来设置数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆