[R] Getting minimum value of a column according a factor column of a dataframe

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Thu Aug 25 17:54:21 CEST 2022


Hello,

Inline.

Às 16:37 de 25/08/2022, avi.e.gross using 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 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.

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

> 
> 
> 
> -----Original Message-----
> From: R-help <r-help-bounces using r-project.org> On Behalf Of Ebert,Timothy Aaron
> Sent: Thursday, August 25, 2022 8:28 AM
> To: Ebert,Timothy Aaron <tebert using ufl.edu>; javad bayat
> <j.bayat194 using gmail.com>; Rui Barradas <ruipbarradas using sapo.pt>
> Cc: R-help using 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 using r-project.org> On Behalf Of Ebert,Timothy Aaron
> Sent: Thursday, August 25, 2022 8:18 AM
> To: javad bayat <j.bayat194 using gmail.com>; Rui Barradas <ruipbarradas using sapo.pt>
> Cc: R-help using 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 using r-project.org> On Behalf Of javad bayat
> Sent: Thursday, August 25, 2022 12:56 AM
> To: Rui Barradas <ruipbarradas using sapo.pt>
> Cc: R-help using 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 using sapo.pt> wrote:
> 
>> 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;
>>> 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.
>>>
>>>> x[which(x$Q == min(x$Q)),]
>>>
>>> Sincerely
>>>
>>>
>>>
>>
> 
> 
> --
> Best Regards
> Javad Bayat
> M.Sc. Environment Engineering
> Alternative Mail: bayat194 using yahoo.com
> 
>          [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help using 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&data=05%7C01%7Ctebert%40ufl.edu%7Cfc547
> a8912984c0b118208da8693f860%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637
> 970267341688438%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIi
> LCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=GvFzbrCZcSiitmSGdae
> uB9A7I2nVYMILrjYsXOMhX8g%3D&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&data=05%7C01%7Ctebert%40ufl.edu%7Cfc547a8912
> 984c0b118208da8693f860%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C63797026
> 7341688438%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBT
> iI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=N9LufwUGQWGKUaPqZ2UYi7yF
> m32mFp%2BivWCUzKFOTtw%3D&reserved=0
> and provide commented, minimal, self-contained, reproducible code.
> 
> ______________________________________________
> R-help using 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&data=05%7C01%7Ctebert%40ufl.edu%7Cfc547
> a8912984c0b118208da8693f860%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637
> 970267341688438%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIi
> LCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=GvFzbrCZcSiitmSGdae
> uB9A7I2nVYMILrjYsXOMhX8g%3D&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&data=05%7C01%7Ctebert%40ufl.edu%7Cfc547a8912
> 984c0b118208da8693f860%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C63797026
> 7341688438%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBT
> iI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=N9LufwUGQWGKUaPqZ2UYi7yF
> m32mFp%2BivWCUzKFOTtw%3D&reserved=0
> and provide commented, minimal, self-contained, reproducible code.
> 
> ______________________________________________
> R-help using 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.
> 
> ______________________________________________
> R-help using 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.



More information about the R-help mailing list