ikuzar
2012-Jan-12 22:42 UTC
[R] how to select column wich median is in this interval [5;6]
Hello, I have got a data frame df like this :> dfe1 e2 e3 e4 1 1 11 1 21 2 2 12 2 22 3 3 13 3 23 4 4 14 4 24 5 5 15 5 25 6 6 16 6 26 7 7 17 7 27 8 8 18 8 28 9 9 19 9 29 10 10 20 10 30 where e1 ... e3 are vectors I have to select columns which median is in the interval [5;6] ( Here, for instance, e1 and e3) I would not want to use loop... (for, while) I search for smthing like this : e_candidates = df[, ] But I do not know how to write the condition, Thanks for your help Thanks for your help I'd like to know how to select -- View this message in context: http://r.789695.n4.nabble.com/how-to-select-column-wich-median-is-in-this-interval-5-6-tp4290745p4290745.html Sent from the R help mailing list archive at Nabble.com.
R. Michael Weylandt
2012-Jan-13 03:14 UTC
[R] how to select column wich median is in this interval [5;6]
Not sure it's doable entirely without a for-loop, but I'd suggest something like df[which( (sapply(df, median) < 6) & (sapply(df, median) > 5))] If your data frame has an enormous amount of rows, perhaps calculate the sapply() call independently and reference the result twice. You can probably do this without the which() call here but I'm doing it defensively to avoid any unexpected behaviors for NAs. Finally, note that I'm leaving out the comma in the [,] call -- it happens to work for data frames here (and it's a good exercise to try to figure out why) but if you want to put it back in, that won't cause problems. Michael On Thu, Jan 12, 2012 at 5:42 PM, ikuzar <razuki at hotmail.fr> wrote:> Hello, > I have got a data frame df like this : >> df > ? e1 e2 e3 e4 > 1 ? 1 11 ?1 21 > 2 ? 2 12 ?2 22 > 3 ? 3 13 ?3 23 > 4 ? 4 14 ?4 24 > 5 ? 5 15 ?5 25 > 6 ? 6 16 ?6 26 > 7 ? 7 17 ?7 27 > 8 ? 8 18 ?8 28 > 9 ? 9 19 ?9 29 > 10 10 20 10 30 > > where e1 ... e3 are vectors > I have to select columns which median is in the interval [5;6] ( Here, for > instance, e1 and e3) > I would not want to use loop... (for, while) > > I search for smthing like this : > e_candidates = df[, ] > But I do not know how to write the condition, > > Thanks for your help > > Thanks for your help > I'd like to know how to select > > -- > View this message in context: http://r.789695.n4.nabble.com/how-to-select-column-wich-median-is-in-this-interval-5-6-tp4290745p4290745.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.