javad bayat
2022-Aug-24 07:44 UTC
[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.> x[which(x$Q == min(x$Q)),]Sincerely -- Best Regards Javad Bayat M.Sc. Environment Engineering Alternative Mail: bayat194 at yahoo.com [[alternative HTML version deleted]]
Ebert,Timothy Aaron
2022-Aug-24 16:16 UTC
[R] Getting minimum value of a column according a factor column of a dataframe
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.> x[which(x$Q == min(x$Q)),]Sincerely -- Best Regards Javad Bayat M.Sc. Environment Engineering Alternative Mail: bayat194 at yahoo.com [[alternative HTML version deleted]] ______________________________________________ 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&data=05%7C01%7Ctebert%40ufl.edu%7C9c40fa5f8b354251cf5c08da85e82878%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637969529406639528%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=wRIHPCENQspDLjF9IbGTsyM1kJ5hYhIwfYuFSU9UFDM%3D&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&data=05%7C01%7Ctebert%40ufl.edu%7C9c40fa5f8b354251cf5c08da85e82878%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637969529406639528%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=jthiGDRGroo5sh8snIDQHHGsWAndtxp5SCB7HvyhWg8%3D&reserved=0 and provide commented, minimal, self-contained, reproducible code.
@vi@e@gross m@iii@g oii gm@ii@com
2022-Aug-24 16:23 UTC
[R] Getting minimum value of a column according a factor column of a dataframe
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.> x[which(x$Q == min(x$Q)),]Sincerely -- Best Regards Javad Bayat M.Sc. Environment Engineering Alternative Mail: bayat194 at yahoo.com [[alternative HTML version deleted]] ______________________________________________ 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.
Rui Barradas
2022-Aug-24 16:54 UTC
[R] Getting minimum value of a column according a factor column of a dataframe
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 > > >