Skip to content
Prev 317436 / 398506 Next

Max value of each 2 rows in dataframe

Hi,

For the dataset A,
Assuming that this is what you meant: the maximum value of "lig" in each 2 rows excluding the first row:

#dataset: A
A[-1,]<-do.call(rbind,lapply(split(A[-1,],((1:nrow(A[-1,])-1)%/%2)+1),function(x){x[4]<-max(x[4]);x}))
?A
#?? ok??????????????? time???? secs lig????? geo???? ind
#1? ok 2010-01-15 04:45:00 40193.45? 28 17016001 5500519
#2? ok 2010-01-15 04:49:59 40193.45? 64 17016001 5500519
#3? ok 2010-01-15 04:54:59 40193.45? 64 17016001 5500519
#4? ok 2010-01-15 04:59:59 40193.46? 32 17016001 5500519
#5? ok 2010-01-15 05:04:59 40193.46? 32 17016001 5500519
#6? ok 2010-01-15 05:09:59 40193.47? 13 17016001 5500519
#7? ok 2010-01-15 05:14:59 40193.47? 13 17016001 5500519
#8? ok 2010-01-15 05:19:59 40193.47?? 8 17016001 5500519
#9? ok 2010-01-15 05:24:59 40193.48?? 8 17016001 5500519
#10 ok 2010-01-15 05:29:59 40193.48?? 0 17016001 5500519
#11 ok 2010-01-15 05:34:59 40193.48?? 0 17016001 5500519
#12 ok 2010-01-15 05:39:59 40193.49? 64 17016001 5500519
#13 ok 2010-01-15 05:44:59 40193.49? 64 17016001 5500519
#14 ok 2010-01-15 05:49:59 40193.49? 64 17016001 5500519
#15 ok 2010-01-15 05:54:59 40193.50? 64 17016001 5500519
#16 ok 2010-01-15 05:59:59 40193.50? 64 17016001 5500519
#17 ok 2010-01-15 06:04:59 40193.50? 64 17016001 5500519
#18 ok 2010-01-15 06:09:59 40193.51? 51 17016001 5500519
#19 ok 2010-01-15 06:14:59 40193.51? 51 17016001 5500519
#20 ok 2010-01-15 06:19:59 40193.51? 64 17016001 5500519

#If you need a rolling max,
library(zoo)
rollapply(A$lig,2,max,align="right",fill=NA)
?#[1] NA 64 64 44 32 22 13? 4? 8? 8? 0 45 64 64 64 64 64 64 51 64

?
A.K.





----- Original Message -----
From: zuzana zajkova <zuzulaz at gmail.com>
To: Rui Barradas <ruipbarradas at sapo.pt>
Cc: r-help at r-project.org
Sent: Tuesday, February 12, 2013 6:53 PM
Subject: Re: [R] Max value of each 2 rows in dataframe

Hi,

sorry for not useable data...

When I tried to use the dput function to the dataframe let's call it A, had
some problems with the time and clock variables... however I attach an
other dataframe B, which is created by merging A with other dataframe. The
B dataframe has a little different structure as I mentioned before about B,
each time is kind of doubled by variable "fix".

I realized also that probably the most simple way would be to apply the
changes directly to the original source dataframe (has 119 313 rows), which
is later used for creating other dataframes.
Here I attach few lines (hope this format will work well), data are grabbed
every 5 minutes. For this data I would like to save the first row as it is
and from there save the max value of next 5 and 10 minutes.
example: the 3rd line should get the "lig" value equal to max of "lig"
value of line 2 and 3 and so on


structure(list(ok = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "ok", class =
"factor"),
? ? time = structure(c(1263548700, 1263548999, 1263549299, 1263549599,
? ? 1263549899, 1263550199, 1263550499, 1263550799, 1263551099,
? ? 1263551399, 1263551699, 1263551999, 1263552299, 1263552599,
? ? 1263552899, 1263553199, 1263553499, 1263553799, 1263554099,
? ? 1263554399), class = c("POSIXct", "POSIXt"), tzone = ""),
? ? secs = c(40193.447917, 40193.451389, 40193.454861, 40193.458333,
? ? 40193.461806, 40193.465278, 40193.46875, 40193.472222, 40193.475694,
? ? 40193.479167, 40193.482639, 40193.486111, 40193.489583, 40193.493056,
? ? 40193.496528, 40193.5, 40193.503472, 40193.506944, 40193.510417,
? ? 40193.513889), lig = c(28L, 64L, 44L, 32L, 22L, 13L, 4L,
?  0L, 8L, 0L, 0L, 45L, 64L, 64L, 64L, 47L, 64L, 50L, 51L,
? ? 64L), geo = c(17016001, 17016001, 17016001, 17016001, 17016001,
? ? 17016001, 17016001, 17016001, 17016001, 17016001, 17016001,
? ? 17016001, 17016001, 17016001, 17016001, 17016001, 17016001,
? ? 17016001, 17016001, 17016001), ind = c(5500519, 5500519,
? ? 5500519, 5500519, 5500519, 5500519, 5500519, 5500519, 5500519,
? ? 5500519, 5500519, 5500519, 5500519, 5500519, 5500519, 5500519,
? ? 5500519, 5500519, 5500519, 5500519)), .Names = c("ok", "time",
"secs", "lig", "geo", "ind"), row.names = c(NA, 20L), class = "data.frame")

looks like this:

ok? ? ? ? ? ? ? time? ?  secs lig? ? ? geo? ?  ind
1? ok 15/01/10 10:45:00 40193.45? 28 17016001 5500519
2? ok 15/01/10 10:49:59 40193.45? 64 17016001 5500519
3? ok 15/01/10 10:54:59 40193.45? 44 17016001 5500519
4? ok 15/01/10 10:59:59 40193.46? 32 17016001 5500519
5? ok 15/01/10 11:04:59 40193.46? 22 17016001 5500519
6? ok 15/01/10 11:09:59 40193.47? 13 17016001 5500519
7? ok 15/01/10 11:14:59 40193.47? 4 17016001 5500519
8? ok 15/01/10 11:19:59 40193.47? 0 17016001 5500519
9? ok 15/01/10 11:24:59 40193.48? 8 17016001 5500519
10 ok 15/01/10 11:29:59 40193.48? 0 17016001 5500519
11 ok 15/01/10 11:34:59 40193.48? 0 17016001 5500519
12 ok 15/01/10 11:39:59 40193.49? 45 17016001 5500519
13 ok 15/01/10 11:44:59 40193.49? 64 17016001 5500519
14 ok 15/01/10 11:49:59 40193.49? 64 17016001 5500519
15 ok 15/01/10 11:54:59 40193.50? 64 17016001 5500519
16 ok 15/01/10 11:59:59 40193.50? 64 17016001 5500519
17 ok 15/01/10 12:04:59 40193.50? 47 17016001 5500519
18 ok 15/01/10 12:09:59 40193.51? 64 17016001 5500519
19 ok 15/01/10 12:14:59 40193.51? 60 17016001 5500519
20 ok 15/01/10 12:19:59 40193.51? 51 17016001 5500519



___

There are data for "act" each 10 minutes, for "lig" each 5 minutes.
What I would like to get are just the rows with information for each 10
minutes (the "act" time), but I don't want just to erase the rows with NA
values. Before that, my aim is this:
- if "act" value (6th column) is not NA, into the "lig" column (in the same
row) I need to get the maximum value for "lig" of the same row and? one row
before.
for example: in the line 1231: act==0, lig==6, lig in the line 1230 is 17.
I would like it to became in line 1231: act==0, lig==17.

#this is example of B dataframe
structure(list(geo = c(17016001, 17016001, 17016001, 17016001,
17016001, 17016001, 17016001, 17016001, 17016001, 17016001, 17016001,
17016001, 17016001, 17016001, 17016001, 17016001, 17016001, 17016001,
17016001, 17016001), jul = c(14648, 14648, 14648, 14648, 14648,
14648, 14648, 14648, 14648, 14648, 14648, 14648, 14648, 14648,
14648, 14648, 14648, 14648, 14648, 14648), ind.x = c(5500519,
5500519, 5500519, 5500519, 5500519, 5500519, 5500519, 5500519,
5500519, 5500519, 5500519, 5500519, 5500519, 5500519, 5500519,
5500519, 5500519, 5500519, 5500519, 5500519), time =
structure(c(1265583898,
1265583898, 1265584198, 1265584198, 1265584498, 1265584498, 1265584798,
1265584798, 1265585098, 1265585098, 1265585398, 1265585398, 1265585698,
1265585698, 1265585998, 1265585998, 1265586298, 1265586298, 1265586598,
1265586598), class = c("POSIXct", "POSIXt"), tzone = ""), act = c(200,
200, NA, NA, 200, 200, NA, NA, 200, 200, NA, NA, 200, 200, NA,
NA, 200, 200, NA, NA), lig = c(0L, 64L, 16L, 0L, 14L, 43L, 22L, 0L,
1L, 0L, 0L, 3L, 0L, 6L, 0L, 0L, 9L, 23L, 64L, 44L), date.x = c("08/02/10",
"08/02/10", "08/02/10", "08/02/10", "08/02/10", "08/02/10", "08/02/10",
"08/02/10", "08/02/10", "08/02/10", "08/02/10", "08/02/10", "08/02/10",
"08/02/10", "08/02/10", "08/02/10", "08/02/10", "08/02/10", "08/02/10",
"08/02/10"), h = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), m = c(4L, 4L, 9L, 9L, 14L,
14L, 19L, 19L, 24L, 24L, 29L, 29L, 34L, 34L, 39L, 39L, 44L, 44L,
49L, 49L), s = c(58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L,
58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L), d = c(8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L), mo = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), y = c(10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L), dtime = c(0.0827777777777778, 0.0827777777777778,
0.166111111111111, 0.166111111111111, 0.249444444444444, 0.249444444444444,
0.332777777777778, 0.332777777777778, 0.416111111111111, 0.416111111111111,
0.499444444444444, 0.499444444444444, 0.582777777777778, 0.582777777777778,
0.666111111111111, 0.666111111111111, 0.749444444444444, 0.749444444444444,
0.832777777777778, 0.832777777777778), land = c(0, 0, NA, NA,
0, 0, NA, NA, 0, 0, NA, NA, 0, 0, NA, NA, 0, 0, NA, NA), sriseIC =
structure(c(59L,
59L, 59L, 59L, 59L, 59L, 59L, 59L, 59L, 59L, 59L, 59L, 59L, 59L,
59L, 59L, 59L, 59L, 59L, 59L), .Label = c("7:09", "7:10", "7:11",
"7:12", "7:13", "7:14", "7:15", "7:16", "7:17", "7:18", "7:19",
"7:20", "7:21", "7:22", "7:23", "7:24", "7:25", "7:26", "7:27",
"7:28", "7:29", "7:30", "7:31", "7:32", "7:33", "7:34", "7:35",
"7:36", "7:37", "7:38", "7:39", "7:40", "7:41", "7:42", "7:43",
"7:44", "7:45", "7:46", "7:47", "7:48", "7:49", "7:50", "7:51",
"7:52", "7:53", "7:54", "7:55", "7:56", "7:57", "7:58", "7:59",
"8:00", "8:01", "8:02", "8:03", "8:04", "8:05", "8:06", "8:07",
"8:08", "8:09"), class = "factor"), ssetIC = structure(c(35L,
35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L,
35L, 35L, 35L, 35L, 35L, 35L), .Label = c("19:05", "19:06", "19:07",
"19:08", "19:09", "19:10", "19:11", "19:12", "19:13", "19:14",
"19:15", "19:16", "19:17", "19:18", "19:19", "19:20", "19:21",
"19:22", "19:23", "19:24", "19:25", "19:26", "19:27", "19:28",
"19:29", "19:30", "19:31", "19:32", "19:33", "19:34", "19:35",
"19:36", "19:37", "19:38", "19:39", "19:40", "19:41", "19:42",
"19:43", "19:44", "19:45", "19:46", "19:47", "19:48", "19:49",
"19:50", "19:51", "19:52", "19:53", "19:54", "19:55", "19:56",
"19:57", "19:58", "19:59", "20:00", "20:01", "20:02", "20:03",
"20:04", "20:05", "20:06", "20:07", "20:08", "20:09", "20:10",
"20:11", "20:12"), class = "factor"), dssetIC = c(19.65, 19.65,
19.65, 19.65, 19.65, 19.65, 19.65, 19.65, 19.65, 19.65, 19.65,
19.65, 19.65, 19.65, 19.65, 19.65, 19.65, 19.65, 19.65, 19.65
), dsriseIC = c(8.11666666666667, 8.11666666666667, 8.11666666666667,
8.11666666666667, 8.11666666666667, 8.11666666666667, 8.11666666666667,
8.11666666666667, 8.11666666666667, 8.11666666666667, 8.11666666666667,
8.11666666666667, 8.11666666666667, 8.11666666666667, 8.11666666666667,
8.11666666666667, 8.11666666666667, 8.11666666666667, 8.11666666666667,
8.11666666666667), fix = structure(c(1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label =
c("midnight",
"noon"), class = "factor"), dawn = structure(c(NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_), .Label = c("07:06", "07:07",
"07:08", "07:09", "07:10", "07:11", "07:12", "07:13", "07:14",
"07:15", "07:16", "07:17", "07:18", "07:19", "07:20", "07:21",
"07:22", "07:23", "07:24", "07:25", "07:26", "07:27", "07:28",
"07:29", "07:30", "07:31", "07:32", "07:33", "07:34", "07:35",
"07:36", "07:37", "07:38", "07:39", "07:40", "07:41", "07:42",
"07:43", "07:44", "07:45", "07:46", "07:47", "07:48", "07:49",
"07:50", "07:51", "07:52", "07:53", "07:54", "07:55", "07:56",
"07:57", "07:58", "07:59", "08:00", "08:01", "08:02", "08:03",
"08:04", "08:05", "08:06", "08:07", "08:08", "08:09", "08:11",
"08:12", "08:18", "15:27"), class = "factor"), dusk =
structure(c(NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_), .Label = c("15:40",
"19:01", "19:03", "19:04", "19:06", "19:07", "19:08", "19:09",
"19:10", "19:11", "19:12", "19:13", "19:14", "19:15", "19:16",
"19:17", "19:18", "19:19", "19:20", "19:21", "19:22", "19:23",
"19:24", "19:25", "19:26", "19:27", "19:28", "19:29", "19:30",
"19:31", "19:32", "19:33", "19:34", "19:35", "19:36", "19:37",
"19:38", "19:39", "19:40", "19:41", "19:42", "19:43", "19:44",
"19:45", "19:46", "19:47", "19:48", "19:49", "19:50", "19:51",
"19:52", "19:53", "19:54", "19:55", "19:56", "19:57", "19:58",
"19:59", "20:00", "20:01", "20:02", "20:03", "20:04", "20:05",
"20:06", "20:08", "20:09", "20:10", "20:13", "20:14", "20:15",
"20:16", "20:23", "20:29", "20:30", "20:34", "20:35", "20:36",
"20:38", "20:40", "20:42", "20:43", "20:44", "20:45", "20:46",
"20:47", "20:48", "20:49", "20:50", "20:52", "20:53", "20:54",
"20:55", "20:56", "20:57", "20:58", "20:59", "21:00", "21:01",
"21:02", "21:03", "21:04", "21:05", "21:06", "21:07", "21:08",
"21:09", "21:10", "21:12", "21:13", "21:15", "21:17", "21:18"
), class = "factor"), lat = c(14.97, 14.97, 14.97, 14.97, 14.97,
14.97, 14.97, 14.97, 14.97, 14.97, 14.97, 14.97, 14.97, 14.97,
14.97, 14.97, 14.97, 14.97, 14.97, 14.97), long = c(-24.64, -24.64,
-24.64, -24.64, -24.64, -24.64, -24.64, -24.64, -24.64, -24.64,
-24.64, -24.64, -24.64, -24.64, -24.64, -24.64, -24.64, -24.64,
-24.64, -24.64), ddawn = c(8.11666666666667, 8.11666666666667,
8.11666666666667, 8.11666666666667, 8.11666666666667, 8.11666666666667,
8.11666666666667, 8.11666666666667, 8.11666666666667, 8.11666666666667,
8.11666666666667, 8.11666666666667, 8.11666666666667, 8.11666666666667,
8.11666666666667, 8.11666666666667, 8.11666666666667, 8.11666666666667,
8.11666666666667, 8.11666666666667), ddusk = c(19.65, 19.65,
19.65, 19.65, 19.65, 19.65, 19.65, 19.65, 19.65, 19.65, 19.65,
19.65, 19.65, 19.65, 19.65, 19.65, 19.65, 19.65, 19.65, 19.65
), Year = structure(c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("2007/2008",
"2008/2009", "2009/2010", "2010/2011", "2011/2012"), class = "factor"),
? ? MPNJ = c(14725, 14725, 14725, 14725, 14725, 14725, 14725,
? ? 14725, 14725, 14725, 14725, 14725, 14725, 14725, 14725, 14725,
? ? 14725, 14725, 14725, 14725), WINJ = c(14727, 14727, 14727,
? ? 14727, 14727, 14727, 14727, 14727, 14727, 14727, 14727, 14727,
? ? 14727, 14727, 14727, 14727, 14727, 14727, 14727, 14727),
? ? MPRJ = c(14837, 14837, 14837, 14837, 14837, 14837, 14837,
? ? 14837, 14837, 14837, 14837, 14837, 14837, 14837, 14837, 14837,
? ? 14837, 14837, 14837, 14837), BREJ = c(14840, 14840, 14840,
? ? 14840, 14840, 14840, 14840, 14840, 14840, 14840, 14840, 14840,
? ? 14840, 14840, 14840, 14840, 14840, 14840, 14840, 14840),
? ? DEPJ = c(14648, 14648, 14648, 14648, 14648, 14648, 14648,
? ? 14648, 14648, 14648, 14648, 14648, 14648, 14648, 14648, 14648,
? ? 14648, 14648, 14648, 14648), RECJ = c(15036, 15036, 15036,
? ? 15036, 15036, 15036, 15036, 15036, 15036, 15036, 15036, 15036,
? ? 15036, 15036, 15036, 15036, 15036, 15036, 15036, 15036),
? ? fen = c("bre", "bre", "bre", "bre", "bre", "bre", "bre",
? ? "bre", "bre", "bre", "bre", "bre", "bre", "bre", "bre", "bre",
? ? "bre", "bre", "bre", "bre"), day = c(0, 0, 0, 0, 0, 0, 0,
? ? 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("geo",
"jul", "ind.x", "time", "act", "lig", "date.x", "h", "m", "s",
"d", "mo", "y", "dtime", "land", "sriseIC", "ssetIC", "dssetIC",
"dsriseIC", "fix", "dawn", "dusk", "lat", "long", "ddawn", "ddusk",
"Year", "MPNJ", "WINJ", "MPRJ", "BREJ", "DEPJ", "RECJ", "fen",
"day"), row.names = c(NA, 20L), class = "data.frame")


Thank you.

Zuzana
On 12 February 2013 19:05, Rui Barradas <ruipbarradas at sapo.pt> wrote:

            
??? [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.