[R] Selection with changing number of columns

Allan Engelhardt allane at cybaea.com
Tue Jul 6 15:16:07 CEST 2010


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.
>



More information about the R-help mailing list