kxk
2009-Jul-24 05:31 UTC
[R] How to find the min and max of two variables in a data frame
I have two variables in a data frame, I want to generate two additional variables. For every observations (i.e. every row), I want the first new variable 'min' to carry the minimum of the two existing variables, and I want the second new variable 'max' to carry the maximum of the two existing variables. I then want to sort the data frame by min and max, and delete duplicated rows if both rows has the same min and same max. I am new to R so not sure how to fix my code. Here is my attempt and it is not working. Thanks! edge_dir$min=edge_dir[if (edge_dir$cid_dir1 < edeg_dir$cid_dir2) edge_dir$cid_dir1 else edge_dir$cid_dir2] edge_dir$max=edge_dir[if (edge_dir$cid_dir1 > edeg_dir$cid_dir2) edge_dir$cid_dir1 else edge_dir$cid_dir2] -- View this message in context: http://www.nabble.com/How-to-find-the-min-and-max-of-two-variables-in-a-data-frame-tp24638856p24638856.html Sent from the R help mailing list archive at Nabble.com.
Dimitris Rizopoulos
2009-Jul-24 06:29 UTC
[R] How to find the min and max of two variables in a data frame
try this: set.seed(123) dat <- data.frame(x = round(rnorm(10)), y = round(rnorm(10))) dat$Min <- pmin(dat$x, dat$y) dat$Max <- pmax(dat$x, dat$y) dat ind <- dat$Min != dat$Max dat[ind, ] I hope it helps. Best, Dimitris kxk wrote:> I have two variables in a data frame, I want to generate two additional > variables. For every observations (i.e. every row), I want the first new > variable 'min' to carry the minimum of the two existing variables, and I > want the second new variable 'max' to carry the maximum of the two existing > variables. I then want to sort the data frame by min and max, and delete > duplicated rows if both rows has the same min and same max. > > I am new to R so not sure how to fix my code. Here is my attempt and it is > not working. Thanks! > > edge_dir$min=edge_dir[if (edge_dir$cid_dir1 < edeg_dir$cid_dir2) > edge_dir$cid_dir1 else edge_dir$cid_dir2] > edge_dir$max=edge_dir[if (edge_dir$cid_dir1 > edeg_dir$cid_dir2) > edge_dir$cid_dir1 else edge_dir$cid_dir2] >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014
Bill.Venables at csiro.au
2009-Jul-24 09:02 UTC
[R] How to find the min and max of two variables in a data frame
edge_dir <- within(edge_dir, { vmin <- pmin(cid_dir1, cid_dir2) vmax <- pmax(cid_dir1, cid_dir2) }) ________________________________________ From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] On Behalf Of kxk [kkong at u.washington.edu] Sent: 24 July 2009 15:31 To: r-help at r-project.org Subject: [R] How to find the min and max of two variables in a data frame I have two variables in a data frame, I want to generate two additional variables. For every observations (i.e. every row), I want the first new variable 'min' to carry the minimum of the two existing variables, and I want the second new variable 'max' to carry the maximum of the two existing variables. I then want to sort the data frame by min and max, and delete duplicated rows if both rows has the same min and same max. I am new to R so not sure how to fix my code. Here is my attempt and it is not working. Thanks! edge_dir$min=edge_dir[if (edge_dir$cid_dir1 < edeg_dir$cid_dir2) edge_dir$cid_dir1 else edge_dir$cid_dir2] edge_dir$max=edge_dir[if (edge_dir$cid_dir1 > edeg_dir$cid_dir2) edge_dir$cid_dir1 else edge_dir$cid_dir2] -- View this message in context: http://www.nabble.com/How-to-find-the-min-and-max-of-two-variables-in-a-data-frame-tp24638856p24638856.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at r-project.org mailing list 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.