基于每日最大值来设置数据帧 [英] Subsetting a dataframe based on daily maxima
本文介绍了基于每日最大值来设置数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
数据看起来像这样:
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屋!
查看全文