Get value from other row in same group in r - r

I have the following dataset:
date_nba <- tibble::tribble(
~idGame, ~slugTeam, ~slugOpponent, ~drebTeam, ~orebTeam,
20900001, "NOP", "TOR", 37, 16,
20900001, "TOR", "NOP", 41, 16,
20900002, "LAL", "LAC", 32, 9,
20900002, "LAC", "LAL", 34, 11
)
I want to create a column called drebOpp, which is the drebTeam from the other slugTeam in the same idGame. So the desired result would be:
tibble::tribble(
~idGame, ~slugTeam, ~slugOpponent, ~drebTeam, ~orebTeam, ~drebOpp,
20900001, "NOP", "TOR", 37, 16, 41,
20900001, "TOR", "NOP", 41, 16, 37,
20900002, "LAL", "LAC", 32, 9, 34,
20900002, "LAC", "LAL", 34, 11, 32
)
I know there's probably an easy solution using group_by and mutate, but I can't seem to find it anywhere. Any help would be appreciated!

We can do a group by match
library(dplyr)
date_nba %>%
group_by(idGame) %>%
mutate(drebOpp = drebTeam[match(slugTeam, slugOpponent)])

Related

Filter dates to show only the current month and 12 months ahead in R

I have a df with some dates and I would like to filter dates to show only the current month and 12 months ahead.
This is my df:
I would like to keep, for each date in the Date column, in the DataReferencia column, the dates of the current month and 12 months ahead and then subtract the values from the Value column. For the above dates, on the day 2003-01-17, it would be the dates in the DataReferencia column 2003-01-01 and 2003-12-01. This df runs from 2003-01 to 2020-12.
I tried this code, but returns an empty df:
library(dplyr)
library(lubridate)
test %>%
filter(year(DataReferencia) == Data.Ano & month(DataReferencia) == Data.Mes + 11,
month(DataReferencia) == Data.Mes)
My dput:
structure(list(Instituicao = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1), Data = structure(c(12069, 12069, 12069,
12069, 12069, 12069, 12069, 12069, 12069, 12069, 12069, 12069,
12070, 12070, 12070, 12070, 12070), class = "Date"), DataReferencia = structure(c(12053,
12084, 12112, 12143, 12173, 12204, 12234, 12265, 12296, 12326,
12357, 12387, 12053, 12084, 12112, 12143, 12173), class = "Date"),
Valor = c(26, 24, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
26, 24, 22, 22, 22), DataReuniao = structure(c(12073, 12073,
12073, 12073, 12073, 12073, 12073, 12073, 12073, 12073, 12073,
12073, 12073, 12073, 12073, 12073, 12073), class = "Date"),
Reuniao = c(80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
80, 80, 80, 80, 80), MetaSelic = c(25.5, 25.5, 25.5, 25.5,
25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5,
25.5, 25.5, 25.5)), row.names = c(NA, 17L), class = "data.frame")
If I understand your question correctly, you want to filter() for dates where the year & month are the same in Data and DataReferencia, or the date in DataReferencia is 11 months ahead of Data. I'm not sure what Data.Ano and Data.Mes are in your failed code, or if these are translated names of the columns names?
This code will do the job:
test %>%
filter(
format(DataReferencia, format = '%Y-%m') == format(Data, format = '%Y-%m')
| format(DataReferencia, format = '%Y-%m') == format(Data + months(11), format = '%Y-%m')
)
# Instituicao Data DataReferencia Valor DataReuniao Reuniao MetaSelic
# 1 1 2003-01-17 2003-01-01 26 2003-01-21 80 25.5
# 2 1 2003-01-17 2003-12-01 22 2003-01-21 80 25.5
# 3 1 2003-01-18 2003-01-01 26 2003-01-21 80 25.5
We use format() to retrieve the date of the data columns in year-month format; we specify this using format = %Y-%m, utilising symbols and abbreviations explained here; basically %Y means the (4-digit) year, and %m is the (2-digit) month. Because this is still in R-recognised date format, it allows the addition of 11 months in the second condition in filter().

Conditionally replace values across multiple columns based on string match in a separate column

I'm trying to conditionally replace values in multiple columns based on a string match in a different column but I'd like to be able to do so in a single line of code using the across() function but I keep getting errors that don't quite make sense to me. I feel like this is probably a simple solution so if anyone could point me in the right direction, that would be fantastic!
df <- data.frame("type" = c("Park", "Neighborhood", "Airport", "Park", "Neighborhood", "Neighborhood"),
"total" = c(34, 56, 75, 89, 21, 56),
"group_a" = c(30, 26, 45, 60, 3, 46),
"group_b" = c(4, 30, 30, 29, 18, 10))
# working but not concise
df %>%
mutate(total = ifelse(str_detect(type, "Park"), NA, total),
group_a = ifelse(str_detect(type, "Park"), NA, group_a),
group_b = ifelse(str_detect(type, "Park"), NA, group_b))
# concise but not working
df %>% mutate(across(total, group_a, group_b), ifelse(str_detect(type, "Park"), NA, .))
Update
We got a solution that works with my dummy dataset but is not working with my real data, so I am going to share a small snippet of my real data frame with the numbers changed and organization names hidden. When I run this line of code (df %>% mutate(across(c(Attempts, Canvasses, Completes)), ~ifelse(str_detect(long_name, "park-cemetery"), NA, .))) on these data, I get the following error message:
Error: Problem with mutate() input ..2. x Input ..2 must be a
vector, not a formula object. i Input ..2 is
~ifelse(str_detect(long_name, "park-cemetery"), NA, .).
This a small sample of the data that produces this error:
df <- structure(list(Org = c("OrgName", "OrgName", "OrgName", "OrgName",
"OrgName", "OrgName", "OrgName", "OrgName", "OrgName", "OrgName"
), nCode = c("M34", "R36", "R46", "X29", "M31", "K39", "Q12",
"Q39", "X41", "K27"), Attempts = c(100, 100, 100, 100, 100, 100,
100, 100, 100, 100), Canvasses = c(80, 80, 80, 80, 80, 80, 80,
80, 80, 80), Completes = c(50, 50, 50, 50, 50, 50, 50, 50, 50,
50), van_nocc_id = c(999, 999, 999, 999, 999, 999, 999, 999,
999, 999), van_name = c("M-Upper West Side", "SI-Rosebank", "SI-Tottenville",
"BX-park-cemetery-etc-Bronx", "M-Stuyvesant Town-Cooper Village",
"BK-Kensington", "Q-Broad Channel", "Q-Lindenwood", "BX-Wakefield",
"BK-East New York"), boro_short = c("M", "SI", "SI", "BX", "M",
"BK", "Q", "Q", "BX", "BK"), long_name = c("Upper West Side",
"Rosebank", "Tottenville", "park-cemetery-etc-Bronx", "Stuyvesant Town-Cooper Village",
"Kensington", "Broad Channel", "Lindenwood", "Wakefield", "East New York"
)), row.names = c(NA, -10L), class = "data.frame")
Final update
The curse of the misplaced closing bracket! Thanks to everyone for your help... the correct solution was df %>% mutate(across(c(Attempts, Canvasses, Completes), ~ifelse(str_detect(long_name, "park-cemetery"), NA, .)))
If you use the newly introduced function across (which is the correct way to approach this task), you have to specify inside across itself the function you want to apply. In this case the function ifelse(...) has to be a purrr-style lambda (so starting with ~). Check out across documentation and look for the arguments .cols and .fns.
df %>%
mutate(across(c(total, group_a, group_b), ~ifelse(str_detect(type, "Park"), NA, .)))
Output
# type total group_a group_b
# 1 Park NA NA NA
# 2 Neighborhood 56 26 30
# 3 Airport 75 45 30
# 4 Park NA NA NA
# 5 Neighborhood 21 3 18
# 6 Neighborhood 56 46 10
Here a data.table solution.
require(data.table)
df <- data.frame("type" = c("Park", "Neighborhood", "Airport", "Park", "Neighborhood", "Neighborhood"),
"total" = c(34, 56, 75, 89, 21, 56),
"group_a" = c(30, 26, 45, 60, 3, 46),
"group_b" = c(4, 30, 30, 29, 18, 10))
setDT(df)
df[type == "Park", c("total", "group_a", "group_b") := NA]
Update: that didn't take long to figure out! Just needed to place the columns in a vector:
# concise AND working!
df %>% mutate(across(c(total, group_a, group_b)), ifelse(str_detect(type, "Park"), NA, .))
I had tried this initially but placed the columns in quotes... don't do that :)

Plotting a picture where dots are categorized by color

I have a data frame and would want to plot a picture thats sorts the data in a certain way.
company <- c("company1","company1","company1","company1","company1","company1","company1","company1","company1","company1","company1",
"company1", "company1", "company1", "company1", "company1",
"company2","company2","company2","company2","company2","company2","company2","company2","company2","company2","company2",
"company2", "company2", "company2", "company2", "company2")
salary <- c(24514, 23775, 24389, 23917, 23422, 23045, 24324, 24079, 22570, 23576, 23005, 23206, 23237, 24955, 22274, 22549,
24655, 24798, 23597, 24921, 23506, 22976, 22967, 24641, 24905, 21413, 24469, 23321, 22858, 22870, 22957, 24646)
age <- c(44, 34, 36, 55, 47, 63, 52, 52, 29, 29, 33, 55, 47, 64, 45, 52,
34, 44, 56, 45, 67, 33, 42, 32, 39, 59, 23, 45, 37, 54, 55, 62)
table <- data.frame(salary=salary, age=age, company=company)
mean_age <- mean(age)
mean_salary <- mean(salary)
Im plotting a picture plot(age, salary).
Now I would want to color all the datapoint that are over the mean_salary while being under the mean_age. If the datapoint belongs to company1 I would want to color it blue and if it belongs to company2 I would want to color it red.
You can do as follow:
# Fist add a column saying if the point should be colored or not
table$should_color=ifelse(table$salary>mean_salary & table$age < mean_age,TRUE,FALSE)
# Then add a color column: black if the point should not be colored,
# and red or blue depending on the company
table$color=ifelse(table$should_color,
ifelse(table$company=="company1","blue","red"),
"black")
# Finally use the color column as plot color
plot(table$age, table$salary,col=table$color)
# Add lines for the mean to check the right part is colored
abline(h=mean_salary,lty=3)
abline(v=mean_age,lty=3)
You can put all the ifelse in one line:
table$color=ifelse(table$salary>mean_salary & table$age < mean_age,
ifelse(table$company=="company1","blue","red"),
"black")

Calculate prop.test() p-value and effect size pairs in aggregated dataframe

After running an experiment, I gathered data and reshaped in this format:
library(tidyverse)
df <- tibble::tribble(
~element, ~sessions, ~begin, ~complete,
"baseline", 256, 67, 15,
"variation", 580, 167, 22
)
Than I aggregated as:
df %>%
group_by(element) %>%
mutate(sessions_to_begin = round(begin/sessions*100,2),
sessions_to_complete = round(complete/sessions*100,2)) -> df_agg
df_agg <- tibble::tribble(
~element, ~sessions, ~begin, ~complete, ~sessions_to_begin, ~sessions_to_complete,
"baseline", 256, 67, 15, 26.17, 5.86,
"variation", 580, 167, 22, 28.79, 3.79
)
I want to achieve create a df that contains a proportion test p-value for the pairs baseline/variation for the metrics session_to_begin and sessions_to_complete.
It would look like:
df_test <- tibble::tribble(
~metric, ~baseline, ~variation, ~p-value, ~effect_size,
"session_to_begin", 25.9, 29.5, "-", "-",
"session_to_complete", 6.03, 3.95, "-", "-"
)
I assume that for the effect size this could be a viable option:
https://rdrr.io/cran/pwr/man/ES.h.html

How to aggregate dataframe by minimum daily value

I am trying to extract values in a large data frame (with per minute data) where I want a subset of the data frame when x variable is at its daily minimum and maximum.
For an example data set see below. The example I have given has daily values to reduce the complexity - so in this case how would I get the monthly data frame values when temperature is at its minimum and maximum.
I do not have a great deal of experience in aggregate but I have been able to extract the monthly min and max temp values:
a <- as.data.frame(aggregate(df$Temp, df[4], function(x) {
c(max = max(x), min = min(x)) }))
But I am not sure how to do this without losing the information from the original data frame - it would almost be a subset based on a minimum and maximum argument? But I am not sure how to write that.
Any help would be appreciated - and apologies for the large dput example.
Thanks
df <- structure(list(Group.1 = c(1628, 1629, 1630, 1631, 1632, 1633,
1634, 1635, 1636, 1637, 1638, 1639, 1640, 1641, 1642, 1643, 1644,
1645, 1646, 1647, 1648, 1649, 1650, 1651, 1652, 1653, 1654, 1655,
1656, 1657, 1658, 1659, 1660, 1661, 1662, 1663, 1664, 1665, 1666,
1667, 1668, 1669, 1670, 1671, 1672, 1673, 1674, 1675, 1676),
datetime = structure(c(1466078376.13352, 1466164800, 1466251194.49235,
1466337600, 1466423992.99026, 1466510400, 1466596853.49096,
1466683185.13551, 1466769600, 1466856000.06254, 1466942345.50765,
1467028800, 1467115179.92356, 1467201600, 1467288000, 1467374400,
1467460801.81376, 1467547200, 1467633604.67316, 1467720000,
1467806423.20361, 1467892800, 1467979255.99444, 1468065552.68428,
1468152000, 1468238400, 1468324827.121, 1468411200, 1468497619.36762,
1468584000, 1468670456.74548, 1468756798.41446, 1468843200,
1468928779.09091, 1469016500.50633, 1469102400, 1469188805.17385,
1469275200, 1469361564.70097, 1469448000, 1469534423.82046,
1469620800, 1469707247.98331, 1469793556.6064, 1469880000,
1469966391.40473, 1470041370, 1470178341.25, 1470217984.93671
), class = c("POSIXct", "POSIXt")), year = c(2016, 2016,
2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016, 2016, 2016, 2016), month = c(6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 8, 8, 8), day = c(16, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 28, 29, 30, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
23, 24, 25, 26, 27, 28, 29, 30, 31, 1, 2, 3), hour = c(11.4937413073713,
11.5, 11.4979137691238, 11.5, 11.4979137691238, 11.5, 11.5146036161335,
11.4961779013204, 11.5, 11.5003474635163, 11.4853963838665,
11.5, 11.4940931202224, 11.5, 11.5, 11.5, 11.5003474635163,
11.5, 11.5006954102921, 11.5, 11.5066018068103, 11.5, 11.5159944367177,
11.4867872044506, 11.5, 11.5, 11.5076495132128, 11.5, 11.505211952745,
11.5, 11.5159944367177, 11.4993045897079, 11.5, 11.2755681818182,
11.6385372714487, 11.5, 11.5020862308762, 11.5, 11.4895688456189,
11.5, 11.5066109951287, 11.5, 11.5132127955494, 11.4881780250348,
11.5, 11.4979137691238, 8.3314447592068, 22.3854166666667,
9.38818565400844), min = c(29.4777468706537, 29.5, 29.5333796940195,
29.5, 29.5083449235049, 29.5, 29.5152990264256, 29.4815844336345,
29.5, 29.4801945795691, 29.4680111265647, 29.5, 29.5198054204309,
29.5, 29.5, 29.5, 29.5093815149409, 29.5, 29.5361613351878,
29.5, 29.4906184850591, 29.5, 29.4735744089013, 29.5041724617524,
29.5, 29.5, 29.4930458970793, 29.5, 29.5100764419736, 29.5,
29.4860917941586, 29.5152990264256, 29.5, 29.2840909090909,
29.5295358649789, 29.5, 29.4610570236439, 29.5, 29.5375521557719,
29.5, 29.500347947112, 29.5, 29.5069541029207, 29.4860917941586,
29.5, 29.4819193324061, 29.1133144475921, 28.7291666666667,
29.2911392405063), sec = c(30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30), DOY = c(167.499723767578,
168.5, 169.499936254057, 170.5, 171.499918868799, 172.5,
173.500619108329, 174.499827957301, 175.5, 176.500000723882,
177.499369301499, 178.5, 179.499767633773, 180.5, 181.5,
182.5, 183.500020992587, 184.5, 185.500054087467, 186.5,
187.500268560343, 188.5, 189.500648083758, 190.499452364395,
191.5, 192.5, 193.500313900479, 194.5, 195.500224162227,
196.5, 197.500656776387, 198.499981648895, 199.5, 200.490498737374,
201.505792897328, 202.5, 203.500059882553, 204.5, 205.499591446453,
206.5, 207.50027569976, 208.5, 209.500555362386, 210.499497759233,
211.5, 212.499900517694, 213.367708333333, 214.953023726852,
215.41186269339), Day = c(1628.49972376758, 1629.5, 1630.49993625406,
1631.5, 1632.4999188688, 1633.5, 1634.50061910833, 1635.4998279573,
1636.5, 1637.50000072388, 1638.4993693015, 1639.5, 1640.49976763377,
1641.5, 1642.5, 1643.5, 1644.50002099259, 1645.5, 1646.50005408747,
1647.5, 1648.50026856034, 1649.5, 1650.50064808376, 1651.49945236439,
1652.5, 1653.5, 1654.50031390048, 1655.5, 1656.50022416223,
1657.5, 1658.50065677639, 1659.4999816489, 1660.5, 1661.49049873737,
1662.50579289733, 1663.5, 1664.50005988255, 1665.5, 1666.49959144645,
1667.5, 1668.50027569976, 1669.5, 1670.50055536239, 1671.49949775923,
1672.5, 1673.49990051769, 1674.36770833333, 1675.95302372685,
1676.41186269339), DayH = c(1628.47890588781, 1629.47916666667,
1630.47907974038, 1631.47916666667, 1632.47907974038, 1633.47916666667,
1634.47977515067, 1635.47900741256, 1636.47916666667, 1637.47918114431,
1638.47855818266, 1639.47916666667, 1640.47892054668, 1641.47916666667,
1642.47916666667, 1643.47916666667, 1644.47918114431, 1645.47916666667,
1646.4791956421, 1647.47916666667, 1648.47944174195, 1649.47916666667,
1650.47983310153, 1651.47861613352, 1652.47916666667, 1653.47916666667,
1654.47948539638, 1655.47916666667, 1656.47938383136, 1657.47916666667,
1658.47983310153, 1659.47913769124, 1660.47916666667, 1661.46981534091,
1662.48493905298, 1663.47916666667, 1664.47925359295, 1665.47916666667,
1666.47873203523, 1667.47916666667, 1668.4794421248, 1669.47916666667,
1670.47971719981, 1671.47867408438, 1672.47916666667, 1673.47907974038,
1674.34714353163, 1675.93272569444, 1676.39117440225), DayD = c(1628,
1629, 1630, 1631, 1632, 1633, 1634, 1635, 1636, 1637, 1638,
1639, 1640, 1641, 1642, 1643, 1644, 1645, 1646, 1647, 1648,
1649, 1650, 1651, 1652, 1653, 1654, 1655, 1656, 1657, 1658,
1659, 1660, 1661, 1662, 1663, 1664, 1665, 1666, 1667, 1668,
1669, 1670, 1671, 1672, 1673, 1674, 1675, 1676), Sal = c(29.0488087045063,
29.0242089236389, 28.9704142552782, 28.9337236612778, 28.9124731267455,
28.8621913531181, 28.7694603506606, 28.6800432876789, 28.6368648858889,
28.5239238692008, 28.6274684358136, 28.6899766423333, 28.7094982390063,
28.6427974009653, 28.6540963996528, 28.5762126331528, 28.5617631359555,
28.6364940399097, 28.6458543031711, 28.603713066875, 28.65796586713,
28.6926140346389, 28.6470827340195, 28.5985220503964, 28.6620416165972,
28.6305870582222, 28.7001961730876, 28.6916165265, 28.6656868092356,
28.8356597378472, 29.266969874235, 28.418354432114, 28.3670879136597,
28.7002130192898, 28.1320816093038, 27.4618531637569, 27.5341382380668,
27.4453546479236, 28.2270356746662, 28.3642271282222, 27.9785534427697,
28.1165695540903, 28.6652365165229, 28.4222878245758, 28.4388172580139,
28.0149544998192, 28.4748151350047, 28.0933474488542, 27.8804163691308
), Temp = c(-0.902727819368567, -0.824054421545139, -0.720653055488178,
-0.64214159655, -0.557226600257997, -0.438045884220833, -0.395580348047288,
-0.295740618513551, -0.262320095793056, -0.160162734756081,
-0.20336842434701, -0.197846770197222, -0.133355481749131,
-0.0565102243486111, -0.0215979124673611, 0.125483112529167,
0.238572840179291, 0.272823149654167, 0.311757436682198,
0.392257187272917, 0.396628769779013, 0.475891600833333,
0.607289245171071, 0.644559951482615, 0.665206005440278,
0.727629137738889, 0.696052211995828, 0.752315860946528,
0.832433359182071, 0.471555079075, -0.187290608750348, 1.12906111324131,
1.24940833146181, 0.780415736372869, 2.0088144469993, 2.93373915290972,
3.19907420429903, 3.16430728506875, 1.87296632160014, 1.74067924683403,
2.50500692939318, 2.26088904221181, 1.20761122894784, 1.64661297725591,
1.68971783634167, 2.8011089384096, 1.70051260145987, 2.9201268373125,
3.38838510550127), Den = c(1023.3705136356, 1023.34875807153,
1023.30254908484, 1023.27139922778, 1023.25147035396, 1023.20777893194,
1023.13225399791, 1023.05719108895, 1023.02118660833, 1022.92671791522,
1023.01148932893, 1023.06187513264, 1023.07546443363, 1023.01910884861,
1023.02718869861, 1022.95948791944, 1022.94312314663, 1023.00176208264,
1023.00818908971, 1022.97111798194, 1023.01464410354, 1023.03946157917,
1022.99742747636, 1022.95676902782, 1023.00684491736, 1022.97892510208,
1023.03639020376, 1023.02648772361, 1023.00193750904, 1023.15208127222,
1023.5269599694, 1022.78984692211, 1022.74222825486, 1023.03256159162,
1022.51158633193, 1021.90175907847, 1021.95317423018, 1021.88198156806,
1022.59607175591, 1022.71358182847, 1022.35521831037, 1022.47925275556,
1022.98340138456, 1022.76567206815, 1022.77633019236, 1022.36132367177,
1022.80426542115, 1022.42016046875, 1022.21591253502), Chl = c(0.426618683276773,
0.415201663443056, 0.385096423775383, 0.363738871729861,
0.409404512937413, 0.434480798338889, 0.491604291206537,
0.464091430292564, 0.519904672929861, 0.549126851486449,
0.552805036465925, 0.557850158557639, 0.466429236588603,
0.510509837531944, 0.488934344839583, 0.3889341805625, 0.423089474131341,
0.499932285390278, 0.459906828269124, 0.365046922974306,
0.275903841463516, 0.249863925111806, 0.234845544844924,
0.225590052227399, 0.230629094999306, 0.214579363215278,
0.166625286886648, 0.171862695222222, 0.188135247756776,
0.2714899376625, 0.249608510288595, 0.234688186765647, 0.263209856711111,
0.223578790403409, 0.196086601016174, 0.300936248076389,
0.198270502905424, 0.254613127958333, 0.211631943366481,
0.194650132228472, 0.221967901366736, 0.287470503101389,
0.346767555667594, 0.272033042223922, 0.261851759145139,
0.30292882953338, 0.354326095381492, 0.139364686395833, 0.25231837455865
), O2 = c(8.40147872631572, 8.38937171346181, 8.41514077121905,
8.42152294330694, 8.47538879331154, 8.49880474437986, 8.47472190401391,
8.57655982897498, 8.61944234794514, 8.61577329576095, 8.69213896446314,
8.68921609862778, 8.69141974274149, 8.70994810763125, 8.68851865927569,
8.67396664622847, 8.76928090276998, 8.80177919844583, 8.80529066242629,
8.7617663811118, 8.70471848277554, 8.73156013737708, 8.76644351992003,
8.7618030795612, 8.79754675150208, 8.79138352644236, 8.75052502651043,
8.71269655839444, 8.68668315413829, 8.60423627197361, 8.6258442449847,
8.54381038276356, 8.66501514202014, 8.70410267486577, 8.4057585319121,
8.04968271129722, 7.92461467095619, 7.9134090372875, 8.41306227992907,
8.53320737933681, 8.33820448514335, 8.38417834865208, 8.62405400836439,
8.42477547407102, NaN, 8.40141680898052, 8.48576496565722,
8.28582430035417, 7.96892147137215), ID = c(1.21133857408067,
1.16902903935, 1.11473135400904, 1.07275230797986, 1.03223155043115,
0.995775405476389, 0.962935072082058, 0.91925791728214, 0.844455992659028,
0.791515976553162, 0.751516800569541, 0.702131858139583,
0.707145974122307, 0.599273624570139, 0.502570098100694,
0.347712868368056, 0.335673459499653, 0.152877532414716,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), PAR = c(23.0673615360132,
32.1793662073708, 6.5780126555751, 26.73609441565, 48.6703905053255,
31.0519168669979, 32.4282362099165, 62.9277536311577, 42.349564172909,
36.1037009880507, 36.4754793940695, 41.2935785894486, 56.3411955151001,
32.0717618120021, 68.055826105709, 83.7724642720521, 92.4702250095455,
63.3510742622049, 123.118356252679, 127.241395072853, 134.184346299723,
144.712733899262, 152.801023269344, 145.640278660995, 149.976623338179,
159.125758044126, 185.67869649515, 154.650267769035, 118.350412925882,
97.6856430502583, 120.50500187914, 114.336144557309, 113.572030429865,
164.301402493774, 129.582420528291, 116.032838587456, 129.230413097896,
74.3598830959583, 156.888302579191, 146.709144185685, 170.418761938188,
109.206260157938, 107.575876383898, 116.456387998532, 101.787857506992,
138.683838377278, 59.2571219263966, 301.257992689583, 132.693163343932
), CO2 = c(313.287072418802, 316.458148259666, 311.875193224513,
310.725188107302, 307.113608999861, 305.345432183449, 306.866938424095,
296.338500576548, 301.685137467594, 301.496499046973, 292.31953961727,
288.615012611822, 290.213867048504, 285.695949347705, 287.207517193185,
288.916130984771, 284.025547924356, 280.331734920028, 276.455368840947,
283.035881080528, 288.114431753027, 281.591903017038, 277.471364486351,
275.133554284401, 272.650852387413, 276.396523718707, 279.08769762117,
282.910516518289, 287.561712111621, 298.595955828581, 290.299065880292,
280.221814783983, 271.902607398331, 267.655275534974, 187.578839181691,
295.344229054242, 294.793103994916, 299.124436790682, 275.716171689137,
271.748266282198, 274.022120842021, 276.591247188178, 268.405941518524,
267.118504699234, 265.624798023783, 275.240563041295, 266.857200690445,
79.6423144553947, 283.663994869992), IR = c(94.888329799096,
168.205345239028, 43.6012952622392, 155.376871086389, 249.315080590056,
188.485805169097, 140.05749463426, 327.30331523419, 317.749437070833,
335.839690080959, 288.73672165751, 315.9134794, 342.093960744267,
147.913321086785, 265.198319784722, 299.562780926042, 349.640961553857,
170.956328942361, 328.451052432198, 347.751372498958, 334.37511897533,
334.446311627778, 329.363355990612, 326.623001649513, 322.837199274444,
322.997446772569, 325.385474717316, 280.920902051319, 190.673680428353,
126.069364682462, 163.4228148, 153.172728333727, 166.565107412222,
251.82414954041, 241.950038763854, 186.326751968743, 252.239057356328,
114.703921665806, 231.317110343331, 217.123805467431, 263.428218097425,
150.468188479832, 152.846154624771, 164.441859235949, 149.983140035384,
190.64123323394, 86.6026193321907, 449.640322291667, 216.572470091966
), Wind = c(1.98109184856853, 4.53879830464599, 8.70032103138219,
6.59899662613967, 2.86950158330462, 1.91094036908824, 2.49644956157983,
4.51135762214745, 2.42873628334266, 1.82804668840925, 2.79143010703083,
2.16237533031289, 2.15848082990616, 2.82829126926611, 3.04592042806289,
4.43295455205038, 3.64900015248738, 5.93927428289323, 4.568747849922,
4.15613471303208, 2.91637168187178, 2.52179220100977, 1.44870759127444,
1.39712269197199, 2.07052311200698, 3.19886056692388, 4.59176673645728,
4.23987138922819, 6.5241126458074, 7.15729601227273, 5.78232411651958,
4.8622789499446, 4.40757803569404, 6.01049680807786, 4.37623822535244,
5.64634072332915, 6.9869768911053, 6.16325833892184, 4.01858391688531,
5.13520196430042, 4.98398472755042, 5.54085930760014, 6.1472408154439,
2.8608028792402, 3.73845798467388, 4.77812324101398, 2.78512103174882,
3.31994465129167, 2.24169682706943), AirTemp = c(4.40810744394993,
4.83240431381944, 2.53433683929068, 3.31246568041667, 3.30829270725313,
5.59009755125, 5.59340448650904, 7.34151981681723, 7.95149346486111,
6.35076665670605, 9.06164061349096, 10.7704471338194, 10.8565160628214,
7.49149095395833, 8.37798674340278, 6.50827215368056, 5.0225399762335,
7.40747958826389, 9.64073649255911, 9.69248163631944, 12.3917031494788,
14.0608254371528, 15.2186511161335, 17.322914922114, 17.6249810152778,
17.0803095690972, 15.2016485305981, 13.1528769309028, 12.8485860753996,
9.93430037097222, 8.49772086689847, 9.2253200445758, 7.55898443965278,
7.24284858572443, 6.94021240464135, 5.91370547875, 4.38009589235049,
5.42976892055556, 7.28829746376912, 6.63289298236111, 6.79095412268615,
9.45812934520833, 9.2179658081363, 10.4900065421419, 9.56056064826389,
10.5006926291377, 9.92910989490085, 11.8657420520833, 11.0606573877637
), Press = c(1010.94713220445, 1000.91298554167, 987.981428212796,
1003.31839191667, 1010.80608631433, 1010.70622420833, 1014.20480271905,
1018.25909922168, 1017.2977571875, 1017.2116500139, 1014.43729111961,
1016.86667607639, 1017.77638776233, 1017.73254004861, 1017.58831039583,
1023.96083316667, 1023.37822454482, 1018.61740789583, 1017.69460385953,
1019.06393609722, 1019.70283468381, 1021.57185751389, 1022.54906664812,
1019.93420589013, 1015.60680218056, 1013.92687847222, 1018.10282961752,
1017.75759179861, 1011.06972191105, 1000.6753998125, 1006.33157139777,
1010.47605901252, 1004.2360214375, 1003.47128640625, 1005.9544021519,
1002.94591790972, 998.129144311544, 994.845405819444, 996.572536383866,
1007.62132047917, 1015.58149314544, 1015.31391086806, 1014.80282973574,
1014.86646130737, 1011.12184847917, 1007.64412461752, 1008.94791512748,
1016.38731552083, 1014.23599833755), Ts = c(0.0939091805522304,
0.0933571324389885, 0.0926315816934272, 0.0920807064972713,
0.0914849244286843, 0.0906487390808381, 0.0903508008513881,
0.0896503398391888, 0.0894158788365697, 0.0886992027701387,
0.089002314704399, 0.0889635640417059, 0.0885111330762104,
0.0879720536464657, 0.0877271381692247, 0.0866953766257083,
0.0859020961040837, 0.0856618525704464, 0.0853887527876392,
0.0848241204226848, 0.0847934498305695, 0.0842374892345861,
0.0833158824031102, 0.0830544806008765, 0.0829096777186984,
0.0824718703347108, 0.0826933403725865, 0.0822988121621235,
0.0817369399215142, 0.0842685237873781, 0.0888896196011912,
0.079656634577284, 0.0788127760214962, 0.0821017792323535,
0.0734881871639127, 0.0670067820465301, 0.065144627947714,
0.0653888659545473, 0.0744405166847922, 0.0753679790243025,
0.0700098285152706, 0.0717215346483428, 0.0791058326941815,
0.0760274126969679, 0.0757251777215326, 0.0679345115120072,
0.0756495928144236, 0.0670994071000443, 0.0638174503450037
), C = c(8.94065543989075, 8.91870846791228, 8.89129215782166,
8.87041424509199, 8.84686764433126, 8.81516998854457, 8.8084814071784,
8.78498979939825, 8.77801254243485, 8.75556135353215, 8.76169863848835,
8.75623839427317, 8.73659381953759, 8.71875839369915, 8.7081454153551,
8.67125827547341, 8.64042586696951, 8.62643884773293, 8.61505698749588,
8.59531077426103, 8.59085475620887, 8.56693513415321, 8.53362826949292,
8.5263455666875, 8.51701535738585, 8.50192437544613, 8.50643587795999,
8.49224025025525, 8.47229959883608, 8.56396191736222, 8.71907216880554,
8.40708575426135, 8.37898781319225, 8.48450883536676, 8.19834292777621,
8.02742642860397, 7.94335742141408, 7.96038019864108, 8.22636890956846,
8.25191633282365, 8.08596024446269, 8.14084822127661, 8.37247267374788,
8.27175018847621, 8.25980001007453, 8.01532360653014, 8.25581090260999,
7.97597778883369, 7.87850954070042), deltaO = c(-0.539176713575035,
-0.529336754450473, -0.476151386602607, -0.448891301785048,
-0.371478851019715, -0.316365244164706, -0.333759503164495,
-0.20842997042327, -0.158570194489716, -0.139788057771207,
-0.0695596740252039, -0.0670222956453951, -0.0451740767961,
-0.00881028606789499, -0.0196267560794031, 0.00270837075506498,
0.12885503580047, 0.1753403507129, 0.190233674930407, 0.166455606850779,
0.11386372656667, 0.164625003223876, 0.23281525042711, 0.235457512873693,
0.280531394116234, 0.289459150996232, 0.244089148550445,
0.22045630813919, 0.214383555302208, 0.0402743546113934,
-0.0932279238208418, 0.136724628502207, 0.286027328827889,
0.219593839499005, 0.207415604135885, 0.0222562826932507,
-0.0187427504578891, -0.0469711613535759, 0.18669337036061,
0.281291046513154, 0.25224424068066, 0.243330127375477, 0.251581334616514,
0.153326651891512, NaN, 0.285793738567012, 0.229954063047229,
0.30984651152048, 0.0904119306717343), k = c(1.3843960648632,
6.67024147531018, 21.4898676969447, 12.6511158639218, 2.86455163396347,
1.18964832218751, 2.48536575543307, 6.11457188358869, 2.22117060425211,
1.39709711243724, 3.15681260593466, 1.69087035092879, 1.96031515063071,
3.05646023068449, 3.88091903012736, 5.89522588325253, 4.61191456284459,
9.9876497231258, 5.87859816406069, 5.25567516554151, 2.66410131584561,
1.91750845984603, 0.912508517423828, 0.788437993630362, 1.51297375451523,
4.1010391646833, 6.69386108760172, 6.60202002300452, 15.3120378300758,
17.0865393002387, 10.0683520284002, 7.04605349774694, 7.06555793241213,
11.2155892631143, 6.95932652757971, 10.5460039186381, 14.4457642190408,
11.1713965641408, 5.13491665227681, 9.17952853060783, 7.55395359942885,
9.93963153178716, 10.7556273136677, 2.74976182917367, 4.24852415082047,
7.44862825836769, 2.51232305022297, 3.16831051334729, 1.58255842462718
), NCP = c(-6555.51823916664, -30259.5423200574, -93284.1485540249,
-52785.5627897263, -9203.47328702618, -3474.14113092716,
-6643.58632076862, -11084.7222504768, -2779.86321684949,
-1481.582285219, -1974.24446012626, -1018.46398725869, -689.919166266737,
1680.8563208385, -442.391803921574, -180.816469873293, 5330.66701930139,
16192.8444708129, 10544.1353158852, 8370.10647897766, 2991.63246971473,
2741.5747232174, 1716.98452107368, 1723.27625157814, 3806.41762190115,
10674.6146528716, 15783.1240720338, 14337.3594334794, 30669.7204136871,
3257.08033198347, -8574.53718630027, 10409.6993939132, 17974.5471722579,
22284.8034527993, 11849.079774671, 7706.03700970845, 2652.66574996729,
-10421.7431381603, 8448.71926418016, 24212.1390873852, 18754.1407791307,
21358.338661014, 22616.1137584938, 3695.2416322836, NaN,
18437.8290676373, 4903.94862723591, 8770.18358118303, 1206.37283774814
)), .Names = c("Group.1", "datetime", "year", "month", "day",
"hour", "min", "sec", "DOY", "Day", "DayH", "DayD", "Sal", "Temp",
"Den", "Chl", "O2", "ID", "PAR", "CO2", "IR", "Wind", "AirTemp",
"Press", "Ts", "C", "deltaO", "k", "NCP"), row.names = 1324:1372, class
= "data.frame")
If the output you are looking for is that subset of df rows having maximum or minimum Temp among all rows with the same month value then:
subset(df, ave(Temp, month, FUN = function(x) x %in% range(x)) == 1)

Resources