The requirements keep being clarified and it would have been very useful to
know more in advance.
To be clear. My earlier suggestion was based on JUST wanting the minimum for
each unique version of Code. Then you wanted it in the original order so that
was handled by carefully making that a
Hi Javad,
In that case, just modify the function to extract the rows with both
the minimum and maximum Q from each station
df1<-read.table(text="Code Y M D Q N O
41003 81 1 19 0.16 7.17 2.5
41003 77 9 22 0.197 6.8 2.2
41003 79 7 28 0.21 4.7 6.2
41005 79 8 17 0.21 5.5 7.2
41005 80 10 30
Yes, Timothy, the request was not seen by all of us as the same.
Indeed if the request was to show a subset of the original data consisting
of only the rows that were the minimum for each Code and also showed ties,
then the solution is a tad more complex. I would then do something along the
lines
The order of the rows returned by summarize is controlled by the levels of
the factors given to group_by. If group_by is given character columns
instead of factors, it converts them to factors with the levels being the
sorted unique values of the character columns. Convert you character
columns
Hello,
Inline.
Às 16:37 de 25/08/2022, avi.e.gr...@gmail.com escreveu:
I read all the replies and am not sure why nobody used what I see as simpler
and more direct.
Assuming the ORDER of the output matters, it tends to be controlled by the
order of the factor called Code so I have simple code
I read all the replies and am not sure why nobody used what I see as simpler
and more direct.
Assuming the ORDER of the output matters, it tends to be controlled by the
order of the factor called Code so I have simple code like this:
---
# Load required libraries
library(dplyr)
# Simulate
My mistake, I did not change the sort order back to the original order. If you
do not like the additional variables they can be dropped using select() or
dat2[,-c(RN, MinByCodeQ)] syntax.
library(dplyr)
library(magrittr)
dat2<-read.table(text="Code Y M D Q N O
41003 81 1 19 0.16 7.17 2.5
41003
I missed where you explained how to choose a minimum value if there are several
values within a group that are equal to the minimum value. Here is a dplyr code
that returns eight values because there are ties for minimum values in Q.
library(dplyr)
library(magrittr)
dat2<-read.table(text="Code
Hello,
OK, what about
res <- lapply(split(df1, df1$Code), \(x) x[which.min(x$Q),])
do.call(rbind, res)
# Code Y M D QNO
# 41003 41003 81 1 19 0.160 7.17 2.50
# 41005 41005 79 8 17 0.210 5.50 7.20
# 41009 41009 79 2 21 0.218 5.56 4.04
# 41017 41017 79 10 20 0.240
Javad,
If I understood you, you want to use one of many methods to GROUP BY one
column and take the minimum within each group.
If your data is set up right, perhaps using factors, there are base R
versions but many would also suggest using dplyr/tidyverse methods such as
piping your data to
library(dplyr)
library(magrittr)
dat2<-read.table(text="Code Y M D Q N O
41003 81 1 19 0.16 7.17 2.5
41003 77 9 22 0.197 6.8 2.2
41003 79 7 28 0.21 4.7 6.2
41005 79 8 17 0.21 5.5 7.2
41005 80 10 30 0.21 6.84 2.6
41005 80 12 20 0.21 6.84 2.4
41005 79 6 14 0.217 5.61 3.55
41009 79 2 21 0.218 5.56
11 matches
Mail list logo