Skip to content

Getting minimum value of a column according a factor column of a dataframe

25 messages · Rui Barradas, @vi@e@gross m@iii@g oii gm@ii@com, Ebert,Timothy Aaron +4 more

#
Dear all;
I am trying to get the minimum value of a column based on a factor column
of the same data frame. My data frame is like the below:
       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 4.04
41009 79 5 27 0.218 6.4 3.12
41009 80 11 29 0.22 6.84 2.8
41009 78 5 28 0.232 6 3.2
41009 81 8 20 0.233 6.39 1.6
41009 79 9 30 0.24 5.6 7.5
41017 79 10 20 0.24 5.3 7.1
41017 80 7 30 0.24 6.73 2.6

I want to get the minimum value of the "Q" column with the whole row
values, according to the "Code"  column  which is a factor. Overall it will
give me 4 rows, with the value of "Q". Below is a code that I used but it
did not give me what I wanted.
Sincerely
#
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 4.04
41009 79 5 27 0.218 6.4 3.12
41009 80 11 29 0.22 6.84 2.8
41009 78 5 28 0.232 6 3.2
41009 81 8 20 0.233 6.39 1.6
41009 79 9 30 0.24 5.6 7.5
41017 79 10 20 0.24 5.3 7.1
41017 80 7 30 0.24 6.73 2.6", header=TRUE)
dat3 <- dat2 %>%
  group_by(Code) %>%
  mutate(
    MinByCodeY = min(Y, na.rm = T),
  ) %>%
  arrange(Code)

This returns a dataframe with a new variable "MinByCodeY" which is the minimum value in Y for each group of Code where the minimum value within the group is added to each row of data in the original dataframe.

Tim

-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of javad bayat
Sent: Wednesday, August 24, 2022 3:45 AM
To: R-help at r-project.org
Subject: [R] Getting minimum value of a column according a factor column of a dataframe

[External Email]

Dear all;
I am trying to get the minimum value of a column based on a factor column of the same data frame. My data frame is like the below:
       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 4.04
41009 79 5 27 0.218 6.4 3.12
41009 80 11 29 0.22 6.84 2.8
41009 78 5 28 0.232 6 3.2
41009 81 8 20 0.233 6.39 1.6
41009 79 9 30 0.24 5.6 7.5
41017 79 10 20 0.24 5.3 7.1
41017 80 7 30 0.24 6.73 2.6

I want to get the minimum value of the "Q" column with the whole row values, according to the "Code"  column  which is a factor. Overall it will give me 4 rows, with the value of "Q". Below is a code that I used but it did not give me what I wanted.
Sincerely



--
Best Regards
Javad Bayat
M.Sc. Environment Engineering
Alternative Mail: bayat194 at yahoo.com


______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&amp;data=05%7C01%7Ctebert%40ufl.edu%7C9c40fa5f8b354251cf5c08da85e82878%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637969529406639528%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=wRIHPCENQspDLjF9IbGTsyM1kJ5hYhIwfYuFSU9UFDM%3D&amp;reserved=0
PLEASE do read the posting guide https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&amp;data=05%7C01%7Ctebert%40ufl.edu%7C9c40fa5f8b354251cf5c08da85e82878%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637969529406639528%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=jthiGDRGroo5sh8snIDQHHGsWAndtxp5SCB7HvyhWg8%3D&amp;reserved=0
and provide commented, minimal, self-contained, reproducible code.
#
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 group_by then to generating a report per group using the
function(s) you wish. 

In your case, if all four categories were found in your data, you would get
four output lines.

Note in a very low-tech way, if your problem is static and you know the
exact 4 values you want, you can simply make 4 subsets of your data directly
and apply your minimum calculation to each. If you have a situation with the
number of factors not being known in advance, more general methods that
dynamically do the grouping for you are needed.

-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of javad bayat
Sent: Wednesday, August 24, 2022 3:45 AM
To: R-help at r-project.org
Subject: [R] Getting minimum value of a column according a factor column of
a dataframe

Dear all;
I am trying to get the minimum value of a column based on a factor column of
the same data frame. My data frame is like the below:
       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 4.04
41009 79 5 27 0.218 6.4 3.12
41009 80 11 29 0.22 6.84 2.8
41009 78 5 28 0.232 6 3.2
41009 81 8 20 0.233 6.39 1.6
41009 79 9 30 0.24 5.6 7.5
41017 79 10 20 0.24 5.3 7.1
41017 80 7 30 0.24 6.73 2.6

I want to get the minimum value of the "Q" column with the whole row values,
according to the "Code"  column  which is a factor. Overall it will give me
4 rows, with the value of "Q". Below is a code that I used but it did not
give me what I wanted.
Sincerely



--
Best Regards
Javad Bayat
M.Sc. Environment Engineering
Alternative Mail: bayat194 at yahoo.com


______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.
#
Hello,

Here are two options, the 1st outputs a vector, the 2nd a data.frame.


x<-'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 4.04
41009 79 5 27 0.218 6.4 3.12
41009 80 11 29 0.22 6.84 2.8
41009 78 5 28 0.232 6 3.2
41009 81 8 20 0.233 6.39 1.6
41009 79 9 30 0.24 5.6 7.5
41017 79 10 20 0.24 5.3 7.1
41017 80 7 30 0.24 6.73 2.6'
df1 <- read.table(textConnection(x))
names(df1) <- scan(what = character(),
                    text = 'Code Y M D Q N O')
df1$Code <- factor(df1$Code)

# 1st option
with(df1, tapply(Q, Code, min))
#  41003 41005 41009 41017
#  0.160 0.210 0.218 0.240

# 2nd option
aggregate(Q ~ Code, df1, min)
#     Code     Q
#  1 41003 0.160
#  2 41005 0.210
#  3 41009 0.218
#  4 41017 0.240


Hope this helps,

Rui Barradas

?s 08:44 de 24/08/2022, javad bayat escreveu:
#
Dear all,
Many thanks for your suggested methods and codes, but unfortunately they
did not give the desired results.
All the codes you have provided are correct but they did not represent the
whole row which is related to the minimum of "Q".
The code must result in 4 rows, with the minimum value of "Q" and other
column values, as below:

       Code

              Y

              M

               D

           Q

            N

             O

41003

81

1

19

0.16

7.17

2.5

41005

79

8

17

0.21

5.5

7.2

41009

79

2

21

0.218

5.56

4.04
41017 79 10 20 0.24 5.3 7.1






Sincerely



























































































41017 79 10 20 0.24 5.3 7.1
On Wed, Aug 24, 2022 at 9:24 PM Rui Barradas <ruipbarradas at sapo.pt> wrote:

            

  
    
#
Hello,

OK, what about


res <- lapply(split(df1, df1$Code), \(x) x[which.min(x$Q),])
do.call(rbind, res)
#         Code  Y  M  D     Q    N    O
#  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 5.30 7.10


A dplyr solution.



suppressPackageStartupMessages(library(dplyr))

df1 %>%
   group_by(Code) %>%
   slice_min(Q) %>%
   slice_head(n = 1)
#  # A tibble: 4 ? 7
#  # Groups:   Code [4]
#    Code      Y     M     D     Q     N     O
#    <fct> <int> <int> <int> <dbl> <dbl> <dbl>
#  1 41003    81     1    19 0.16   7.17  2.5
#  2 41005    79     8    17 0.21   5.5   7.2
#  3 41009    79     2    21 0.218  5.56  4.04
#  4 41017    79    10    20 0.24   5.3   7.1



Hope this helps,

Rui Barradas


?s 05:56 de 25/08/2022, javad bayat escreveu:
#
Dear Rui;
Thank you very much. Both of your codes worked correctly. Now I can see the
whole row's value.
But I found a problem in the results. When I run your codes, the results
are shown in a sorted table. I do not know why the results have been sorted
according to the "Code" column, smallest to largest. Is there any way to
get the results like their order in the first data frame (bilan2)? I used
your codes as follow:
Sincerely
On Thu, Aug 25, 2022 at 11:52 AM Rui Barradas <ruipbarradas at sapo.pt> wrote:

            

  
    
#
The data are sorted by the variable(s) in the group_by() statement. If the variable in the group_by() statement is not correct then you can change it to whatever the correct variable may be. If the calculations are correct, but you want a different sort order then arrange() (from dplyr) would work as would order() and sort() commands. There are many help pages on the web for sorting vectors and dataframes.

Tim

-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of javad bayat
Sent: Thursday, August 25, 2022 3:53 AM
To: Rui Barradas <ruipbarradas at sapo.pt>
Cc: R-help at r-project.org
Subject: Re: [R] Getting minimum value of a column according a factor column of a dataframe

[External Email]

Dear Rui;
Thank you very much. Both of your codes worked correctly. Now I can see the whole row's value.
But I found a problem in the results. When I run your codes, the results are shown in a sorted table. I do not know why the results have been sorted according to the "Code" column, smallest to largest. Is there any way to get the results like their order in the first data frame (bilan2)? I used your codes as follow:
Sincerely
On Thu, Aug 25, 2022 at 11:52 AM Rui Barradas <ruipbarradas at sapo.pt> wrote:

            
--
Best Regards
Javad Bayat
M.Sc. Environment Engineering
Alternative Mail: bayat194 at yahoo.com


______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&amp;data=05%7C01%7Ctebert%40ufl.edu%7C50403ccd882345d00db908da8689bacb%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637970223349266197%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=OrqS9I1qav2ztgWpW6BtMkFU9wa0s8dMrVAEP834%2FaU%3D&amp;reserved=0
PLEASE do read the posting guide https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&amp;data=05%7C01%7Ctebert%40ufl.edu%7C50403ccd882345d00db908da8689bacb%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637970223349266197%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=EzG74%2BCS%2FF2ghpT9FKy%2BDm2Qf%2FAARRCJSXxmYBoqoIQ%3D&amp;reserved=0
and provide commented, minimal, self-contained, reproducible code.
#
Hello,

To keep the original order, try


res <- lapply(split(df1, df1$Code), \(x) x[which.min(x$Q),])
res <- do.call(rbind, res)
i <- order(unique(df1$Code))
res[order(i), ]


Hope this helps,

Rui Barradas

?s 08:53 de 25/08/2022, javad bayat escreveu:
#
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 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 4.04
41009 79 5 27 0.218 6.4 3.12
41009 80 11 29 0.22 6.84 2.8
41009 78 5 28 0.232 6 3.2
41009 81 8 20 0.233 6.39 1.6
41009 79 9 30 0.24 5.6 7.5
41017 79 10 20 0.24 5.3 7.1
41017 80 7 30 0.24 6.73 2.6", header=TRUE)
dat2 <- dat2 %>%
  group_by(Code) %>%
  mutate(
    MinByCodeQ = min(Q, na.rm = T),
  ) %>%
  arrange(Code)
dat2<-filter(dat2,Q==MinByCodeQ)



Tim

-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of javad bayat
Sent: Thursday, August 25, 2022 12:56 AM
To: Rui Barradas <ruipbarradas at sapo.pt>
Cc: R-help at r-project.org
Subject: Re: [R] Getting minimum value of a column according a factor column of a dataframe

[External Email]

Dear all,
Many thanks for your suggested methods and codes, but unfortunately they did not give the desired results.
All the codes you have provided are correct but they did not represent the whole row which is related to the minimum of "Q".
The code must result in 4 rows, with the minimum value of "Q" and other column values, as below:

       Code

              Y

              M

               D

           Q

            N

             O

41003

81

1

19

0.16

7.17

2.5

41005

79

8

17

0.21

5.5

7.2

41009

79

2

21

0.218

5.56

4.04
41017 79 10 20 0.24 5.3 7.1






Sincerely



























































































41017 79 10 20 0.24 5.3 7.1
On Wed, Aug 24, 2022 at 9:24 PM Rui Barradas <ruipbarradas at sapo.pt> wrote:

            
--
Best Regards
Javad Bayat
M.Sc. Environment Engineering
Alternative Mail: bayat194 at yahoo.com


______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&amp;data=05%7C01%7Ctebert%40ufl.edu%7Caf44a3eea239431cdd9808da8679e392%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637970155341721228%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=bSKbIIx0Ce14UZCxV3foH%2FtDjAbc3leysR6Uu6mTSCk%3D&amp;reserved=0
PLEASE do read the posting guide https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&amp;data=05%7C01%7Ctebert%40ufl.edu%7Caf44a3eea239431cdd9808da8679e392%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637970155341721228%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=70hiBxaFp9nl0ihQhqkOnBTxUoOv44l4BabLB%2FtVkTg%3D&amp;reserved=0
and provide commented, minimal, self-contained, reproducible code.
#
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 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 4.04
41009 79 5 27 0.218 6.4 3.12
41009 80 11 29 0.22 6.84 2.8
41009 78 5 28 0.232 6 3.2
41009 81 8 20 0.233 6.39 1.6
41009 79 9 30 0.24 5.6 7.5
41017 79 10 20 0.24 5.3 7.1
41017 80 7 30 0.24 6.73 2.6", header=TRUE)
dat2$RN <- rownames(dat2)
dat2 <- dat2 %>%
  group_by(Code) %>%
  mutate(
    MinByCodeQ = min(Q, na.rm = T),
  ) %>%
  arrange(Code)
dat2<-filter(dat2,Q==MinByCodeQ)
dat2<-arrange(dat2,as.numeric(RN))

-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of Ebert,Timothy Aaron
Sent: Thursday, August 25, 2022 8:18 AM
To: javad bayat <j.bayat194 at gmail.com>; Rui Barradas <ruipbarradas at sapo.pt>
Cc: R-help at r-project.org
Subject: Re: [R] Getting minimum value of a column according a factor column of a dataframe

[External Email]

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 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 4.04
41009 79 5 27 0.218 6.4 3.12
41009 80 11 29 0.22 6.84 2.8
41009 78 5 28 0.232 6 3.2
41009 81 8 20 0.233 6.39 1.6
41009 79 9 30 0.24 5.6 7.5
41017 79 10 20 0.24 5.3 7.1
41017 80 7 30 0.24 6.73 2.6", header=TRUE)
dat2 <- dat2 %>%
  group_by(Code) %>%
  mutate(
    MinByCodeQ = min(Q, na.rm = T),
  ) %>%
  arrange(Code)
dat2<-filter(dat2,Q==MinByCodeQ)



Tim

-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of javad bayat
Sent: Thursday, August 25, 2022 12:56 AM
To: Rui Barradas <ruipbarradas at sapo.pt>
Cc: R-help at r-project.org
Subject: Re: [R] Getting minimum value of a column according a factor column of a dataframe

[External Email]

Dear all,
Many thanks for your suggested methods and codes, but unfortunately they did not give the desired results.
All the codes you have provided are correct but they did not represent the whole row which is related to the minimum of "Q".
The code must result in 4 rows, with the minimum value of "Q" and other column values, as below:

       Code

              Y

              M

               D

           Q

            N

             O

41003

81

1

19

0.16

7.17

2.5

41005

79

8

17

0.21

5.5

7.2

41009

79

2

21

0.218

5.56

4.04
41017 79 10 20 0.24 5.3 7.1






Sincerely



























































































41017 79 10 20 0.24 5.3 7.1
On Wed, Aug 24, 2022 at 9:24 PM Rui Barradas <ruipbarradas at sapo.pt> wrote:

            
--
Best Regards
Javad Bayat
M.Sc. Environment Engineering
Alternative Mail: bayat194 at yahoo.com


______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&amp;data=05%7C01%7Ctebert%40ufl.edu%7Cfc547a8912984c0b118208da8693f860%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637970267341688438%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=GvFzbrCZcSiitmSGdaeuB9A7I2nVYMILrjYsXOMhX8g%3D&amp;reserved=0
PLEASE do read the posting guide https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&amp;data=05%7C01%7Ctebert%40ufl.edu%7Cfc547a8912984c0b118208da8693f860%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637970267341688438%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=N9LufwUGQWGKUaPqZ2UYi7yFm32mFp%2BivWCUzKFOTtw%3D&amp;reserved=0
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&amp;data=05%7C01%7Ctebert%40ufl.edu%7Cfc547a8912984c0b118208da8693f860%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637970267341688438%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=GvFzbrCZcSiitmSGdaeuB9A7I2nVYMILrjYsXOMhX8g%3D&amp;reserved=0
PLEASE do read the posting guide https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&amp;data=05%7C01%7Ctebert%40ufl.edu%7Cfc547a8912984c0b118208da8693f860%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637970267341688438%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=N9LufwUGQWGKUaPqZ2UYi7yFm32mFp%2BivWCUzKFOTtw%3D&amp;reserved=0
and provide commented, minimal, self-contained, reproducible 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 reading in from a file by using in-line text version

file_contents <- '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 4.04
41009 79 5 27 0.218 6.4 3.12
41009 80 11 29 0.22 6.84 2.8
41009 78 5 28 0.232 6 3.2
41009 81 8 20 0.233 6.39 1.6
41009 79 9 30 0.24 5.6 7.5
41017 79 10 20 0.24 5.3 7.1
41017 80 7 30 0.24 6.73 2.6'

mydf <- read.table(text=file_contents, header=TRUE)

# Group the results and provide a summary of anything you
# want calculated by group:

mydf %>%
  group_by(Code) %>%
  summarize(minQ=min(Q),
            meanQ=mean(Q),
            maxQ=max(Q),
            sdQ=sd(Q))

----------

Shown as a data.frame, the result is:

   Code  minQ     meanQ  maxQ         sdQ
1 41003 0.160 0.1890000 0.210 0.025942244
2 41005 0.210 0.2117500 0.217 0.003500000
3 41009 0.218 0.2268333 0.240 0.009389711
4 41017 0.240 0.2400000 0.240 0.000000000

You can remove all my extra code to just get the minimum:

mydf %>%
  group_by(Code) %>%
  summarize(minQ=min(Q)) %>% 
  as.data.frame


   Code  minQ
1 41003 0.160
2 41005 0.210
3 41009 0.218
4 41017 0.240

The codes are shown in the same order as the data as it was made into a
factor from raw data in that order.

I may have misunderstood something as others provided an assortment of
methods that, to me, seem less direct. The summarise/summarize function in
dplyr was specifically designed to make a summary as requested, and grouped
if preceded by a request.



-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of Ebert,Timothy Aaron
Sent: Thursday, August 25, 2022 8:28 AM
To: Ebert,Timothy Aaron <tebert at ufl.edu>; javad bayat
<j.bayat194 at gmail.com>; Rui Barradas <ruipbarradas at sapo.pt>
Cc: R-help at r-project.org
Subject: Re: [R] Getting minimum value of a column according a factor column
of a dataframe

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 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 4.04
41009 79 5 27 0.218 6.4 3.12
41009 80 11 29 0.22 6.84 2.8
41009 78 5 28 0.232 6 3.2
41009 81 8 20 0.233 6.39 1.6
41009 79 9 30 0.24 5.6 7.5
41017 79 10 20 0.24 5.3 7.1
41017 80 7 30 0.24 6.73 2.6", header=TRUE) dat2$RN <- rownames(dat2)
dat2 <- dat2 %>%
  group_by(Code) %>%
  mutate(
    MinByCodeQ = min(Q, na.rm = T),
  ) %>%
  arrange(Code)
dat2<-filter(dat2,Q==MinByCodeQ)
dat2<-arrange(dat2,as.numeric(RN))

-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of Ebert,Timothy Aaron
Sent: Thursday, August 25, 2022 8:18 AM
To: javad bayat <j.bayat194 at gmail.com>; Rui Barradas <ruipbarradas at sapo.pt>
Cc: R-help at r-project.org
Subject: Re: [R] Getting minimum value of a column according a factor column
of a dataframe

[External Email]

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 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 4.04
41009 79 5 27 0.218 6.4 3.12
41009 80 11 29 0.22 6.84 2.8
41009 78 5 28 0.232 6 3.2
41009 81 8 20 0.233 6.39 1.6
41009 79 9 30 0.24 5.6 7.5
41017 79 10 20 0.24 5.3 7.1
41017 80 7 30 0.24 6.73 2.6", header=TRUE)
dat2 <- dat2 %>%
  group_by(Code) %>%
  mutate(
    MinByCodeQ = min(Q, na.rm = T),
  ) %>%
  arrange(Code)
dat2<-filter(dat2,Q==MinByCodeQ)



Tim

-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of javad bayat
Sent: Thursday, August 25, 2022 12:56 AM
To: Rui Barradas <ruipbarradas at sapo.pt>
Cc: R-help at r-project.org
Subject: Re: [R] Getting minimum value of a column according a factor column
of a dataframe

[External Email]

Dear all,
Many thanks for your suggested methods and codes, but unfortunately they did
not give the desired results.
All the codes you have provided are correct but they did not represent the
whole row which is related to the minimum of "Q".
The code must result in 4 rows, with the minimum value of "Q" and other
column values, as below:

       Code

              Y

              M

               D

           Q

            N

             O

41003

81

1

19

0.16

7.17

2.5

41005

79

8

17

0.21

5.5

7.2

41009

79

2

21

0.218

5.56

4.04
41017 79 10 20 0.24 5.3 7.1






Sincerely



























































































41017 79 10 20 0.24 5.3 7.1
On Wed, Aug 24, 2022 at 9:24 PM Rui Barradas <ruipbarradas at sapo.pt> wrote:

            
--
Best Regards
Javad Bayat
M.Sc. Environment Engineering
Alternative Mail: bayat194 at yahoo.com


______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.
ch%2Fmailman%2Flistinfo%2Fr-help&amp;data=05%7C01%7Ctebert%40ufl.edu%7Cfc547
a8912984c0b118208da8693f860%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637
970267341688438%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIi
LCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=GvFzbrCZcSiitmSGdae
uB9A7I2nVYMILrjYsXOMhX8g%3D&amp;reserved=0
PLEASE do read the posting guide
https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-proje
ct.org%2Fposting-guide.html&amp;data=05%7C01%7Ctebert%40ufl.edu%7Cfc547a8912
984c0b118208da8693f860%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C63797026
7341688438%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBT
iI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=N9LufwUGQWGKUaPqZ2UYi7yF
m32mFp%2BivWCUzKFOTtw%3D&amp;reserved=0
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.
ch%2Fmailman%2Flistinfo%2Fr-help&amp;data=05%7C01%7Ctebert%40ufl.edu%7Cfc547
a8912984c0b118208da8693f860%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637
970267341688438%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIi
LCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=GvFzbrCZcSiitmSGdae
uB9A7I2nVYMILrjYsXOMhX8g%3D&amp;reserved=0
PLEASE do read the posting guide
https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-proje
ct.org%2Fposting-guide.html&amp;data=05%7C01%7Ctebert%40ufl.edu%7Cfc547a8912
984c0b118208da8693f860%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C63797026
7341688438%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBT
iI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=N9LufwUGQWGKUaPqZ2UYi7yF
m32mFp%2BivWCUzKFOTtw%3D&amp;reserved=0
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.
#
Hello,

Inline.

?s 16:37 de 25/08/2022, avi.e.gross at gmail.com escreveu:
Yes, I agree with you.
In the raw data the Code is already ordered so it doesn't matter if it 
is not addressed in the aggregation code. But from the OP's last post I 
conclude (wrongly?) that in the original data this need not be the case. 
Hence the double order() in my last post. I wonder (a) if it's really 
necessary and (b) if it would make a difference if the aggregated output 
is sorted.

Rui Barradas
#
My assumption (maybe wrong) was that we needed to keep the other variables. I want to find the values of Y, M, D, N, and O for the minimum value of Q within each unique value of Code, keeping the data in the original order. All one need to do is filter Q in the original dataframe by your answer for minQ.

Keeping the data in the original order seems unnecessary, but that is what was asked in a later post.

-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of avi.e.gross at gmail.com
Sent: Thursday, August 25, 2022 11:37 AM
Cc: R-help at r-project.org
Subject: Re: [R] Getting minimum value of a column according a factor column of a dataframe

[External Email]

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 reading in from a file by using in-line text version

file_contents <- '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 4.04
41009 79 5 27 0.218 6.4 3.12
41009 80 11 29 0.22 6.84 2.8
41009 78 5 28 0.232 6 3.2
41009 81 8 20 0.233 6.39 1.6
41009 79 9 30 0.24 5.6 7.5
41017 79 10 20 0.24 5.3 7.1
41017 80 7 30 0.24 6.73 2.6'

mydf <- read.table(text=file_contents, header=TRUE)

# Group the results and provide a summary of anything you # want calculated by group:

mydf %>%
  group_by(Code) %>%
  summarize(minQ=min(Q),
            meanQ=mean(Q),
            maxQ=max(Q),
            sdQ=sd(Q))

----------

Shown as a data.frame, the result is:

   Code  minQ     meanQ  maxQ         sdQ
1 41003 0.160 0.1890000 0.210 0.025942244
2 41005 0.210 0.2117500 0.217 0.003500000
3 41009 0.218 0.2268333 0.240 0.009389711
4 41017 0.240 0.2400000 0.240 0.000000000

You can remove all my extra code to just get the minimum:

mydf %>%
  group_by(Code) %>%
  summarize(minQ=min(Q)) %>%
  as.data.frame


   Code  minQ
1 41003 0.160
2 41005 0.210
3 41009 0.218
4 41017 0.240

The codes are shown in the same order as the data as it was made into a factor from raw data in that order.

I may have misunderstood something as others provided an assortment of methods that, to me, seem less direct. The summarise/summarize function in dplyr was specifically designed to make a summary as requested, and grouped if preceded by a request.



-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of Ebert,Timothy Aaron
Sent: Thursday, August 25, 2022 8:28 AM
To: Ebert,Timothy Aaron <tebert at ufl.edu>; javad bayat <j.bayat194 at gmail.com>; Rui Barradas <ruipbarradas at sapo.pt>
Cc: R-help at r-project.org
Subject: Re: [R] Getting minimum value of a column according a factor column of a dataframe

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 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 4.04
41009 79 5 27 0.218 6.4 3.12
41009 80 11 29 0.22 6.84 2.8
41009 78 5 28 0.232 6 3.2
41009 81 8 20 0.233 6.39 1.6
41009 79 9 30 0.24 5.6 7.5
41017 79 10 20 0.24 5.3 7.1
41017 80 7 30 0.24 6.73 2.6", header=TRUE) dat2$RN <- rownames(dat2)
dat2 <- dat2 %>%
  group_by(Code) %>%
  mutate(
    MinByCodeQ = min(Q, na.rm = T),
  ) %>%
  arrange(Code)
dat2<-filter(dat2,Q==MinByCodeQ)
dat2<-arrange(dat2,as.numeric(RN))

-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of Ebert,Timothy Aaron
Sent: Thursday, August 25, 2022 8:18 AM
To: javad bayat <j.bayat194 at gmail.com>; Rui Barradas <ruipbarradas at sapo.pt>
Cc: R-help at r-project.org
Subject: Re: [R] Getting minimum value of a column according a factor column of a dataframe

[External Email]

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 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 4.04
41009 79 5 27 0.218 6.4 3.12
41009 80 11 29 0.22 6.84 2.8
41009 78 5 28 0.232 6 3.2
41009 81 8 20 0.233 6.39 1.6
41009 79 9 30 0.24 5.6 7.5
41017 79 10 20 0.24 5.3 7.1
41017 80 7 30 0.24 6.73 2.6", header=TRUE)
dat2 <- dat2 %>%
  group_by(Code) %>%
  mutate(
    MinByCodeQ = min(Q, na.rm = T),
  ) %>%
  arrange(Code)
dat2<-filter(dat2,Q==MinByCodeQ)



Tim

-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of javad bayat
Sent: Thursday, August 25, 2022 12:56 AM
To: Rui Barradas <ruipbarradas at sapo.pt>
Cc: R-help at r-project.org
Subject: Re: [R] Getting minimum value of a column according a factor column of a dataframe

[External Email]

Dear all,
Many thanks for your suggested methods and codes, but unfortunately they did not give the desired results.
All the codes you have provided are correct but they did not represent the whole row which is related to the minimum of "Q".
The code must result in 4 rows, with the minimum value of "Q" and other column values, as below:

       Code

              Y

              M

               D

           Q

            N

             O

41003

81

1

19

0.16

7.17

2.5

41005

79

8

17

0.21

5.5

7.2

41009

79

2

21

0.218

5.56

4.04
41017 79 10 20 0.24 5.3 7.1






Sincerely



























































































41017 79 10 20 0.24 5.3 7.1
On Wed, Aug 24, 2022 at 9:24 PM Rui Barradas <ruipbarradas at sapo.pt> wrote:

            
--
Best Regards
Javad Bayat
M.Sc. Environment Engineering
Alternative Mail: bayat194 at yahoo.com


______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz%2F&amp;data=05%7C01%7Ctebert%40ufl.edu%7C8743a664ee564a89657208da86afd075%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637970386928002615%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=G%2BiDn0ShRahazq%2FlGBVgHUe4WxalYK%2B%2BnuEkbS025GU%3D&amp;reserved=0.
ch%2Fmailman%2Flistinfo%2Fr-help&amp;data=05%7C01%7Ctebert%40ufl.edu%7Cfc547
a8912984c0b118208da8693f860%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637
970267341688438%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIi
LCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=GvFzbrCZcSiitmSGdae
uB9A7I2nVYMILrjYsXOMhX8g%3D&amp;reserved=0
PLEASE do read the posting guide
https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-proje%2F&amp;data=05%7C01%7Ctebert%40ufl.edu%7C8743a664ee564a89657208da86afd075%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637970386928002615%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=yb9P82dJF33kgY5ZZiqedyShI16ZQlDA6UWJ6ZJTnQQ%3D&amp;reserved=0
ct.org%2Fposting-guide.html&amp;data=05%7C01%7Ctebert%40ufl.edu%7Cfc547a8912
984c0b118208da8693f860%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C63797026
7341688438%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBT
iI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=N9LufwUGQWGKUaPqZ2UYi7yF
m32mFp%2BivWCUzKFOTtw%3D&amp;reserved=0
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz%2F&amp;data=05%7C01%7Ctebert%40ufl.edu%7C8743a664ee564a89657208da86afd075%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637970386928002615%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=G%2BiDn0ShRahazq%2FlGBVgHUe4WxalYK%2B%2BnuEkbS025GU%3D&amp;reserved=0.
ch%2Fmailman%2Flistinfo%2Fr-help&amp;data=05%7C01%7Ctebert%40ufl.edu%7Cfc547
a8912984c0b118208da8693f860%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637
970267341688438%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIi
LCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=GvFzbrCZcSiitmSGdae
uB9A7I2nVYMILrjYsXOMhX8g%3D&amp;reserved=0
PLEASE do read the posting guide
https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-proje%2F&amp;data=05%7C01%7Ctebert%40ufl.edu%7C8743a664ee564a89657208da86afd075%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637970386928158848%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=ljNbv686YCryrMQezMVFCA7YXLBmEGbg50JKVA9EzX4%3D&amp;reserved=0
ct.org%2Fposting-guide.html&amp;data=05%7C01%7Ctebert%40ufl.edu%7Cfc547a8912
984c0b118208da8693f860%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C63797026
7341688438%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBT
iI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=N9LufwUGQWGKUaPqZ2UYi7yF
m32mFp%2BivWCUzKFOTtw%3D&amp;reserved=0
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&amp;data=05%7C01%7Ctebert%40ufl.edu%7C8743a664ee564a89657208da86afd075%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637970386928158848%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=F5dR15%2BIvRLNJbbq%2Fm6%2B7u9ef6I12HVH4SPEK7fvdNY%3D&amp;reserved=0
PLEASE do read the posting guide https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&amp;data=05%7C01%7Ctebert%40ufl.edu%7C8743a664ee564a89657208da86afd075%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637970386928158848%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=1IcTxl4yCaZWok4Fi6HrOmtoTE78r7Dpfx%2Fod01uifw%3D&amp;reserved=0
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&amp;data=05%7C01%7Ctebert%40ufl.edu%7C8743a664ee564a89657208da86afd075%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637970386928158848%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=F5dR15%2BIvRLNJbbq%2Fm6%2B7u9ef6I12HVH4SPEK7fvdNY%3D&amp;reserved=0
PLEASE do read the posting guide https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&amp;data=05%7C01%7Ctebert%40ufl.edu%7C8743a664ee564a89657208da86afd075%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637970386928158848%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=1IcTxl4yCaZWok4Fi6HrOmtoTE78r7Dpfx%2Fod01uifw%3D&amp;reserved=0
and provide commented, minimal, self-contained, reproducible code.
#
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 to factors with the desired levels to get the order you want.

E.g.,
Large"))
charGroup   x factorGroup
1     Small 101       Small
2     Small 102       Small
3     Small 103       Small
4    Medium 104      Medium
5    Medium 105      Medium
6     Large 106       Large
# A tibble: 3 ? 2
  charGroup  minX
  <chr>     <int>
1 Large       106
2 Medium      104
3 Small       101
# A tibble: 3 ? 2
  factorGroup  minX
  <fct>       <int>
1 Small         101
2 Medium        104
3 Large         106
# A tibble: 4 ? 2
  factorGroup  minX
  <fct>       <dbl>
1 Small         101
2 Medium        104
3 Large         106
4 X Large       Inf
Warning message:
In min(x) : no non-missing arguments to min; returning Inf

-Bill
On Thu, Aug 25, 2022 at 4:05 AM javad bayat <j.bayat194 at gmail.com> wrote:

            

  
  
#
Rui wrote:
<<<<
Yes, I agree with you.
In the raw data the Code is already ordered so it doesn't matter if it is not addressed in the aggregation code. But from the OP's last post I conclude (wrongly?) that in the original data this need not be the case. 
Hence the double order() in my last post. I wonder (a) if it's really necessary and (b) if it would make a difference if the aggregated output is sorted.
My reply (truncated but in earlier messages) has to do with the nature of ordering. We were given what looks like a brief version of some data showing 4 unique values for a column contained in "Code" where the data happened to be in complete order showing several rows with values 41003, then several with 41005 then two more in what looked like numerical ascending order.

My personal guess is that if the Code column is a factor, it is left alone, otherwise it is made into a factor and the order of the factor may not be what you want. I did an experiment of changing one factor to a higher number, and it was moved to the last position in the report even though it was near the top. The default ordering is thus going to result in a summary that may not match what is wanted UNLESS you make adjustments.

ONE METHOD I can think of is to take what I call mydf (the data.frame or tibble you read in) and ask for the unique values of Code like so before making it into a factor:

my_order <- unique(mydf$Code)

I had inserted an 88888 into the data in second position so the result is this:
[1] 41003 88888 41005 41009 41017

The order has been preserved and I can use it to make the order in the factors be the same by using this:

mydf$Code <- factor(mydf$Code, levels=my_order)

It now produces output in that order in the full code shown below:

   Code  minQ
1 41003 0.160
2 88888 0.160
3 41005 0.210
4 41009 0.218
5 41017 0.240

I quickly mention  a second method where you reorder the rows of the output to whatever order you want. I do not show or do that.

NOTE: the code below shows a changed set of data from the original containing an 88888 in row 2 for illustration:
-----CODE-----

# Load required libraries

library(dplyr)

# Simulate reading in from a file by using in-line text version

file_contents <- 'Code Y M D Q N O
41003 81 1 19 0.16 7.17 2.5
88888 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 4.04
41009 79 5 27 0.218 6.4 3.12
41009 80 11 29 0.22 6.84 2.8
41009 78 5 28 0.232 6 3.2
41009 81 8 20 0.233 6.39 1.6
41009 79 9 30 0.24 5.6 7.5
41017 79 10 20 0.24 5.3 7.1
41017 80 7 30 0.24 6.73 2.6'

mydf <- read.table(text=file_contents, header=TRUE)

# Make a factor of column Code in the same order as
# the numbers are first introduced in rows.

my_order <- unique(mydf$Code)

mydf$Code <- factor(mydf$Code, levels=my_order)

# Group the results and provide a summary of anything you
# want calculated by group:

mydf %>%
  group_by(Code) %>%
  summarize(minQ=min(Q)) %>% 
  as.data.frame
#
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 of what others showed such as generating another column showing the
minimum for each row and then showing only rows that matched their value in
two columns or whatever was needed.

As noted, keeping the output in a specific order was not initially
requested.

Keeping the data in some order is a common enough request but in this
situation, I suspect the order many might want would be the one showing the
minimums in order, not the codes in the original order. 

-----Original Message-----
From: Ebert,Timothy Aaron <tebert at ufl.edu> 
Sent: Thursday, August 25, 2022 11:59 AM
To: avi.e.gross at gmail.com
Cc: R-help at r-project.org
Subject: RE: [R] Getting minimum value of a column according a factor column
of a dataframe

My assumption (maybe wrong) was that we needed to keep the other variables.
I want to find the values of Y, M, D, N, and O for the minimum value of Q
within each unique value of Code, keeping the data in the original order.
All one need to do is filter Q in the original dataframe by your answer for
minQ.

Keeping the data in the original order seems unnecessary, but that is what
was asked in a later post.
#
A slightly slicker solution making use of the handy by() function to
avoid the lapply(split...) construction.
Code  Y  M  D     Q    N    O
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 5.30 7.10

This of course ignores the issue of tied minima that Tim Ebert brought
up. That would require a bit more finagling in the anonymous function
code instead of which.min() .

Cheers,
Bert
On Thu, Aug 25, 2022 at 12:22 AM Rui Barradas <ruipbarradas at sapo.pt> wrote:
#
;Dear all
First of all I appreciate you for the answers you have sent. I did the
codes that Rui provided and I got what I wanted.
"
res <- lapply(split(df1, df1$Code), \(x) x[which.min(x$Q),])
res <- do.call(rbind, res)
i <- order(unique(df1$Code))
res[order(i), ]
"
I think I should explain more about my request. I had a large data frame
(11059 rows and 16 columns). The Code column represented the stations code,
totally the number of stations were 128. At each station I had many
measured variables, like Q and N and O, and these variables were measured
in different years and days. The days that data were measured were
different for each station, and due to this reason I had different rows for
stations. For example, station number one (41009) had 158 rows and station
number 2 (41011) had 113 rows. Note that the station's codes are not in
order format (e.g smallest to largest).
Back to my request, I wanted to extract the minimum value of the Q for each
station (based on the Code column). The problem was that I wanted to have
other column values which were measured for this minimum of the Q.
I hope my explanation was clear enough. As I said before, I used the Rui's
codes and I got what I wanted. Although, other solutions provided by others
were all correct.

Regarding my request, unfortunately I faced another problem. I had to
extract the maximum of the Q and put it exactly under the minimum of the Q.
Something like the below one:
"

Code

              Y

              M

               D

           Q

            N

             O

41003

81

1

19

0.16

7.17

2.5

41003

79

8

17

10.21

5.5

7.2

41009

79

2

21

0.218

5.56

4.04
41009 79 10 20 12.24 5.3 7.1
.
.
.
.
"
I extract both min and max according to the codes, and I have 2 dataframes,
one for the minimum values and another for the max values. Both dataframe
have a Code column which is exactly similar.
Can I extract both min and max simultaneously or I have to combine two
dataframes?
I used the rbind and merge function but they did not give the desired
results.
The result of this code adds a second dataframe as columns to the first
one. I want the first row of the second dataframe put below the first row
of the first dataframe and so on. I used a function to do this but it seems
it does not work correctly.
i = 1
                for(i in x) {
                      if (x[i,1] == y[i,1]){
                          rbind(x[i,],y[i,])
                          i = i+1
                }
                }
                }
Sincerely
On Thu, Aug 25, 2022 at 9:08 PM <avi.e.gross at gmail.com> wrote:

            

  
    
#
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 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 4.04
 41009 79 5 27 0.218 6.4 3.12
 41009 80 11 29 0.22 6.84 2.8
 41009 78 5 28 0.232 6 3.2
 41009 81 8 20 0.233 6.39 1.6
 41009 79 9 30 0.24 5.6 7.5
 41017 79 10 20 0.24 5.3 7.1
 41017 80 7 30 0.24 6.73 2.6",
 stringsAsFactors=FALSE,header=TRUE)

# define a function that returns the desired rows
minmaxQrow<-function(x) return(x[c(which.min(x$Q),which.max(x$Q)),])
# apply the function to the data frame
df1a<-by(df1,df1$Code,minmaxQrow)
# set the result to the first element of the list
df1b<-df1a[[1]]
# rbind the remaining rows
for(i in 2:length(df1a)) df1b<-rbind(df1b,df1a[[i]])
# display the result
df1b

Jim
On Fri, Aug 26, 2022 at 5:25 AM javad bayat <j.bayat194 at gmail.com> wrote:
#
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 factor column in the order you wanted the output. Now the request is to throw back in ALL the columns for as many rows as are deemed minimums.

 

So, not in any way demeaning the various methods others offer, I suggest you look at database style joins. R has some in the core with names like merge() and other packages such as dplyr have all kinds of variations on a theme.

 

In my case, you can extend the part of my code that makes this as a second data.frame/tibble:

 

   Code  minQ

1 41003 0.160

2 88888 0.160

3 41005 0.210

4 41009 0.218

5 41017 0.240

 

Call it df2 as in:

 

mydf.min <-

  mydf %>%

  group_by(Code) %>%

  summarize(minQ = min(Q))

 

You now have the original thing I called mydf that has a column called Code and lots of other columns and you have a smaller one with fewer rows and columns called mydf.min and they share a single common column.

 

You want to merge these two using whatever kind of join makes sense. Dplyr offers an inner_join(), left_join(), right_join() and full_join() and you can tweak merge() and others to do similar things.

 

What you seem to want is to find all rows that share both a particular value for Code and at the same time a particular value for minQ in one versus Q in the other. You want to ignore all others.  What gets returned can have all the original columns and perhaps also the minQ column (which can be removed) and if two or more rows in one grouping share exactly the same minimum, may get slightly similar but different lines matched. Is that what you want? Do note matching floating point equally can be dangerous but in this case since the numbers were all just read in, should be fine.

 

You can play with it but I tried this:

 

test <- left_join(mydf.min, mydf, by=c("Code", "minQ" = "Q"))

 

The values returned (and not I added an 88888 category earlier in my data) look like this:

 

Code  minQ  Y  M  D    N    O

1 41003 0.160 81  1 19 7.17 2.50

2 88888 0.160 81  1 19 7.17 2.50

3 41005 0.210 79  8 17 5.50 7.20

4 41005 0.210 80 10 30 6.84 2.60

5 41005 0.210 80 12 20 6.84 2.40

6 41009 0.218 79  2 21 5.56 4.04

7 41009 0.218 79  5 27 6.40 3.12

8 41017 0.240 79 10 20 5.30 7.10

9 41017 0.240 80  7 30 6.73 2.60

 

You can see there are three minimum rows for Code 41005 for example and the join keeps them all.

 

You can trivially remove the minQ column by naming the original as Q or just removing it. You can again reorder things if you wish including by sorting on other columns ascending or descending using other functions/verbs.

 

As noted, this has to work for you with your larger code set and sometimes complex enough such requirements can be done many ways as it can be as much art as science. I personally would probably write most of the above as one long pipeline looking a bit like:

 

mydf <-   

  read.table(?) %>%

  mutate(Code=factor(..., levels=unique(...)))

 

result <-

  mydf %>%

  group_by(Code) %>%

  summarize(...) %>%

  left_join(mydf, by=c("Code", "minQ" = "Q")) %>%

  select(-minQ)

 

And of course it may be better to use the new R pipe operator if your version is new and so on, filling in whatever details make sense to you. At the end of the pipeline, you might want to use verbs that sort the data as described above.

 

My guess is you will now tell us about yet another condition suggestions like mine do not fulfill and I will likely then ignore ?

 

 

 

 

From: javad bayat <j.bayat194 at gmail.com> 
Sent: Thursday, August 25, 2022 2:02 PM
To: avi.e.gross at gmail.com
Cc: R-help at r-project.org
Subject: Re: [R] Getting minimum value of a column according a factor column of a dataframe

 

;Dear all

First of all I appreciate you for the answers you have sent. I did the codes that Rui provided and I got what I wanted.

"

res <- lapply(split(df1, df1$Code), \(x) x[which.min(x$Q),])
res <- do.call(rbind, res)
i <- order(unique(df1$Code))
res[order(i), ] 

"

I think I should explain more about my request. I had a large data frame (11059 rows and 16 columns). The Code column represented the stations code, totally the number of stations were 128. At each station I had many measured variables, like Q and N and O, and these variables were measured in different years and days. The days that data were measured were different for each station, and due to this reason I had different rows for stations. For example, station number one (41009) had 158 rows and station number 2 (41011) had 113 rows. Note that the station's codes are not in order format (e.g smallest to largest). 

Back to my request, I wanted to extract the minimum value of the Q for each station (based on the Code column). The problem was that I wanted to have other column values which were measured for this minimum of the Q. 

I hope my explanation was clear enough. As I said before, I used the Rui's codes and I got what I wanted. Although, other solutions provided by others were all correct. 

 

Regarding my request, unfortunately I faced another problem. I had to extract the maximum of the Q and put it exactly under the minimum of the Q. Something like the below one:

"


Code

              Y

              M

               D

           Q

            N

             O


41003

81

1

19

0.16

7.17

2.5


41003

79

8

17

10.21

5.5

7.2


41009

79

2

21

0.218

5.56

4.04



41009

79

10

20

12.24

5.3

7.1

.

.

.

.

"

I extract both min and max according to the codes, and I have 2 dataframes, one for the minimum values and another for the max values. Both dataframe have a Code column which is exactly similar.

Can I extract both min and max simultaneously or I have to combine two dataframes?

I used the rbind and merge function but they did not give the desired results.
The result of this code adds a second dataframe as columns to the first one. I want the first row of the second dataframe put below the first row of the first dataframe and so on. I used a function to do this but it seems it does not work correctly.
i = 1
                for(i in x) {
                      if (x[i,1] == y[i,1]){
                          rbind(x[i,],y[i,])
                          i = i+1
                } 
                }                                        
                }
Sincerely
On Thu, Aug 25, 2022 at 9:08 PM <avi.e.gross at gmail.com <mailto:avi.e.gross at gmail.com> > wrote:
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 of what others showed such as generating another column showing the
minimum for each row and then showing only rows that matched their value in
two columns or whatever was needed.

As noted, keeping the output in a specific order was not initially
requested.

Keeping the data in some order is a common enough request but in this
situation, I suspect the order many might want would be the one showing the
minimums in order, not the codes in the original order. 

-----Original Message-----
From: Ebert,Timothy Aaron <tebert at ufl.edu <mailto:tebert at ufl.edu> > 
Sent: Thursday, August 25, 2022 11:59 AM
To: avi.e.gross at gmail.com <mailto:avi.e.gross at gmail.com> 
Cc: R-help at r-project.org <mailto:R-help at r-project.org> 
Subject: RE: [R] Getting minimum value of a column according a factor column
of a dataframe

My assumption (maybe wrong) was that we needed to keep the other variables.
I want to find the values of Y, M, D, N, and O for the minimum value of Q
within each unique value of Code, keeping the data in the original order.
All one need to do is filter Q in the original dataframe by your answer for
minQ.

Keeping the data in the original order seems unnecessary, but that is what
was asked in a later post.

______________________________________________
R-help at r-project.org <mailto:R-help at r-project.org>  mailing list -- To UNSUBSCRIBE and more, see
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.
#
Hello,

To return 2 rows for each Code, one for the min and another for the max, 
try the following.

I'm borrowing Bert's by() idea, it makes everything simpler.
There is a hack to have the original Code order kept, since the final 
result res should have two rows for each Code, see what is order()'ed below.


# the output has 2 consecutive rows with
# the same Code, so repeat the unique Codes
i <- order(rep(unique(df1$Code), each = 2))
res <- by(df1, df1$Code, \(x) x[c(which.min(x$Q), which.max(x$Q)), ])
res <- do.call(rbind, res)[order(i), ]

# remake the row names, they're ugly after rbind
row.names(res) <- NULL
res


Hope this helps,

Rui Barradas

?s 19:02 de 25/08/2022, javad bayat escreveu:
#
Dear all;
Many thanks for your suggestions and answers. The problem was solved by the
codes that Rui sent just now. Jim's codes also were correct but did not
give the results in original order.
Dear avi.e.gross at gmail.co,  has provided codes but they just result in
columns with max or min but they did not give the whole row's values
related to the Q min.
Thank you so much again.
Sincerely
On Fri, Aug 26, 2022 at 8:44 AM Rui Barradas <ruipbarradas at sapo.pt> wrote:

            

  
    
#
What you mean, Bayat, is you only read and tried my first reply BEFORE you expanded your request! LOL!

 

I really appreciate reading what other?s write here and different approaches. Rui had a beautiful idea of doubling his rows as a way to retain the order of two requests. It could generalize to more.

 

From: javad bayat <j.bayat194 at gmail.com> 
Sent: Friday, August 26, 2022 12:39 AM
To: Rui Barradas <ruipbarradas at sapo.pt>
Cc: avi.e.gross at gmail.com; R-help at r-project.org
Subject: Re: [R] Getting minimum value of a column according a factor column of a dataframe

 

Dear all;

Many thanks for your suggestions and answers. The problem was solved by the codes that Rui sent just now. Jim's codes also were correct but did not give the results in original order. 

Dear avi.e.gross at gmail.co <mailto:avi.e.gross at gmail.co> ,  has provided codes but they just result in columns with max or min but they did not give the whole row's values related to the Q min.

Thank you so much again.

Sincerely
On Fri, Aug 26, 2022 at 8:44 AM Rui Barradas <ruipbarradas at sapo.pt <mailto:ruipbarradas at sapo.pt> > wrote:
Hello,

To return 2 rows for each Code, one for the min and another for the max, 
try the following.

I'm borrowing Bert's by() idea, it makes everything simpler.
There is a hack to have the original Code order kept, since the final 
result res should have two rows for each Code, see what is order()'ed below.


# the output has 2 consecutive rows with
# the same Code, so repeat the unique Codes
i <- order(rep(unique(df1$Code), each = 2))
res <- by(df1, df1$Code, \(x) x[c(which.min(x$Q), which.max(x$Q)), ])
res <- do.call(rbind, res)[order(i), ]

# remake the row names, they're ugly after rbind
row.names(res) <- NULL
res


Hope this helps,

Rui Barradas

?s 19:02 de 25/08/2022, javad bayat escreveu:

  
    
#
What is the point of this data frame? Beyond the fun of being able to morph data into any shape desired, there is usually some next step. I know that the values in Q are the minimum and maximum for each code, and the first value is the minimum and other value the maximum, but what comes next? If I want to get the difference in Y for the min and max Q I will need to reshape the data (or it would be easier that way). I cannot make a graph that identifies min and max Q, or an equivalent statistical analysis. It appears that no thought has gone into what to do about ties for min or max or what to do if min=max. The latter could happen if there is only one observation for Code or if there is no variability in Q.

While it is very educational for me to see other answers to such problems, I just fail to see the point in the final product in this case because it will take more work to make the data useful. The choices in regards to ties might make the analysis results more (or less) meaningful. Maybe the latter works if Q is a non-decreasing function, though in that case an alternative coding is to select the first and last value for each Code. Using min and max is only relevant if Q sometimes decreases. 

Tim

-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of Rui Barradas
Sent: Friday, August 26, 2022 12:14 AM
To: javad bayat <j.bayat194 at gmail.com>; avi.e.gross at gmail.com
Cc: R-help at r-project.org
Subject: Re: [R] Getting minimum value of a column according a factor column of a dataframe

[External Email]

Hello,

To return 2 rows for each Code, one for the min and another for the max, try the following.

I'm borrowing Bert's by() idea, it makes everything simpler.
There is a hack to have the original Code order kept, since the final result res should have two rows for each Code, see what is order()'ed below.


# the output has 2 consecutive rows with # the same Code, so repeat the unique Codes i <- order(rep(unique(df1$Code), each = 2)) res <- by(df1, df1$Code, \(x) x[c(which.min(x$Q), which.max(x$Q)), ]) res <- do.call(rbind, res)[order(i), ]

# remake the row names, they're ugly after rbind
row.names(res) <- NULL
res


Hope this helps,

Rui Barradas

?s 19:02 de 25/08/2022, javad bayat escreveu:
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&amp;data=05%7C01%7Ctebert%40ufl.edu%7C2cba5775104e4a1277fd08da871974f0%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637970840667682013%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=M1vxIIc8o0fs40Io8KmRUSyEYw1ad8mikRKzBuQd4rg%3D&amp;reserved=0
PLEASE do read the posting guide https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&amp;data=05%7C01%7Ctebert%40ufl.edu%7C2cba5775104e4a1277fd08da871974f0%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637970840667682013%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=vdMExt9qwLs13FqKl7Q2rUVCvTByUSIPYhVySnb5JI8%3D&amp;reserved=0
and provide commented, minimal, self-contained, reproducible code.