Dear list, I'm looking for a way to select rows of a data.frame with changing number of columns (constraint) involved. Assume a data (d) structure like Var.1 Var.2 Var.3 9 2 1 2 9 5 1 2 1 I know the number of involved columns. Is there a way to generate the following selection automatically (maybe for loop), so that it makes no difference if there are two or ten columns involved. Selection: d[d$Var.1==9 | d$Var.1==9 | d$Var.1==9 ,] Does anybody know a way? Thanks Mit freundlichen Gr??en Andreas Kunzler ____________________________ Bundeszahn?rztekammer (BZ?K) Chausseestra?e 13 10115 Berlin Tel.: 030 40005-113 Fax: 030 40005-119 E-Mail: a.kunzler at bzaek.de
I'm not sure your question completely makes sense, but perhaps this will help: set.seed(1) d<- data.frame(matrix(floor(runif(100, max=10)), 10)) # Example data d[apply(d == 9, 1, any), ] # Select rows with 9 in any column ## Or more generally: d[ , c(1, 2, 3)] == c(2, 2, 9) # Or maybe d == 0:9 to select on all columns apply(d[ , c(1, 2, 3)] == c(2, 2, 9), 1, any) # any() being the general `|` function here d[apply(d[ ,c(1, 2, 3)] == c(2, 2, 9), 1, any), ] # Finally: the rows we were looking for A bit over-engineered, perhaps, but gets you to use some generally useful functions. Hope this helps. Allan On 06/07/10 09:33, Kunzler, Andreas wrote:> Dear list, > > I'm looking for a way to select rows of a data.frame with changing number of columns (constraint) involved. > > Assume a data (d) structure like > > > Var.1 Var.2 Var.3 > 9 2 1 > 2 9 5 > 1 2 1 > > I know the number of involved columns. > > Is there a way to generate the following selection automatically (maybe for loop), so that it makes no difference if there are two or ten columns involved. > > Selection: > d[d$Var.1==9 | d$Var.1==9 | d$Var.1==9 ,] > > > Does anybody know a way? > > Thanks > > Mit freundlichen Gr??en > > Andreas Kunzler > ____________________________ > Bundeszahn?rztekammer (BZ?K) > Chausseestra?e 13 > 10115 Berlin > > Tel.: 030 40005-113 > Fax: 030 40005-119 > > E-Mail: a.kunzler at bzaek.de > > ______________________________________________ > 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. >
Try this: d[colSums(d == 9)] On Tue, Jul 6, 2010 at 5:33 AM, Kunzler, Andreas <a.kunzler@bzaek.de> wrote:> Dear list, > > I'm looking for a way to select rows of a data.frame with changing number > of columns (constraint) involved. > > Assume a data (d) structure like > > > Var.1 Var.2 Var.3 > 9 2 1 > 2 9 5 > 1 2 1 > > I know the number of involved columns. > > Is there a way to generate the following selection automatically (maybe for > loop), so that it makes no difference if there are two or ten columns > involved. > > Selection: > d[d$Var.1==9 | d$Var.1==9 | d$Var.1==9 ,] > > > Does anybody know a way? > > Thanks > > Mit freundlichen Grüßen > > Andreas Kunzler > ____________________________ > Bundeszahnärztekammer (BZÄK) > Chausseestraße 13 > 10115 Berlin > > Tel.: 030 40005-113 > Fax: 030 40005-119 > > E-Mail: a.kunzler@bzaek.de > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Tena koe Andreas Assuming you mean d[d$Var.1==9 | d$Var.2==9 | d$Var.3==9 ,] not d[d$Var.1==9 | d$Var.1==9 | d$Var.1==9 ,] then d[apply(d, 1, function(x) any(x==9)),] is one way. HTH .... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Kunzler, Andreas > Sent: Tuesday, 6 July 2010 8:34 p.m. > To: r-help at r-project.org > Subject: [R] Selection with changing number of columns > > Dear list, > > I'm looking for a way to select rows of a data.frame with changing > number of columns (constraint) involved. > > Assume a data (d) structure like > > > Var.1 Var.2 Var.3 > 9 2 1 > 2 9 5 > 1 2 1 > > I know the number of involved columns. > > Is there a way to generate the following selection automatically (maybe > for loop), so that it makes no difference if there are two or ten > columns involved. > > Selection: > d[d$Var.1==9 | d$Var.1==9 | d$Var.1==9 ,]> > > Does anybody know a way? > > Thanks > > Mit freundlichen Gr??en > > Andreas Kunzler > ____________________________ > Bundeszahn?rztekammer (BZ?K) > Chausseestra?e 13 > 10115 Berlin > > Tel.: 030 40005-113 > Fax: 030 40005-119 > > E-Mail: a.kunzler at bzaek.de > > ______________________________________________ > 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.