Dear R Community, A simple problem (for some of you): I wish to index a data.frame by all elements NOT in my index E.g.:> a<-as.data.frame(matrix(rnorm(100),nrow=10,ncol=10)) > b<-which(a$V1>0.8) > b[1] 1 4 6 10> a_indexb<-a[b,] > a_notIndexB<-a[!b,] > nrow(a_notIndexB)[1] 0 Indexing a on b is not a problem (a_indexb), but how can do get only the elements left if I take out the elements indexed with b? Thank you and wishing you a great day! Georg. **************************************** Georg Ehret Baltimore, US [[alternative HTML version deleted]]
On 28.04.2008, at 16:40, Georg Ehret wrote:> E.g.: >> a<-as.data.frame(matrix(rnorm(100),nrow=10,ncol=10)) >> b<-which(a$V1>0.8) >> b > [1] 1 4 6 10 >> a_indexb<-a[b,] >> a_notIndexB<-a[!b,] >> nrow(a_notIndexB) > [1] 0 > > Indexing a on b is not a problem (a_indexb), but how can do get > only the > elements left if I take out the elements indexed with b?The ! operator only works on BOOLEAN. ONE possible way to set a_notIndexB is: a_notIndexB <-a [-1*b, ] --Hans
On Apr 28, 2008, at 10:40 AM, Georg Ehret wrote:> Dear R Community, A simple problem (for some of you): I wish to > index a > data.frame by all elements NOT in my index > E.g.: >> a<-as.data.frame(matrix(rnorm(100),nrow=10,ncol=10)) >> b<-which(a$V1>0.8) >> b > [1] 1 4 6 10 >> a_indexb<-a[b,] >> a_notIndexB<-a[!b,] >> nrow(a_notIndexB) > [1] 0 > > Indexing a on b is not a problem (a_indexb), but how can do get > only the > elements left if I take out the elements indexed with b? >Hi, I think one of the most wonderfully magic things about R is that it can use a logical array to index other things (like a data frame). You could use the extra step with which, but in your case you can bypass that step. Here is a simple example... > a <- data.frame(V1 = seq(-5,5), V2 = seq(-5,5)) > a V1 V2 1 -5 -5 2 -4 -4 3 -3 -3 4 -2 -2 5 -1 -1 6 0 0 7 1 1 8 2 2 9 3 3 10 4 4 11 5 5 > b <- a$V1 > 0 > b [1] FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE > a[b,] V1 V2 7 1 1 8 2 2 9 3 3 10 4 4 11 5 5 > a[!b,] V1 V2 1 -5 -5 2 -4 -4 3 -3 -3 4 -2 -2 5 -1 -1 6 0 0 Hope that helps! Ben
> > a<-as.data.frame(matrix(rnorm(100),nrow=10,ncol=10)) > > b<-which(a$V1>0.8) > > a_indexb<-a[b,] > > a_notIndexB<-a[!b,] > > nrow(a_notIndexB) > [1] 0> a_notIndexB <- a[-b,] > nrow(a_notIndexB)[1] 9 cu Philipp -- Dr. Philipp Pagel Lehrstuhl f?r Genomorientierte Bioinformatik Technische Universit?t M?nchen Wissenschaftszentrum Weihenstephan 85350 Freising, Germany and Institut f?r Bioinformatik und Systembiologie / MIPS Helmholtz Zentrum M?nchen - Deutsches Forschungszentrum f?r Gesundheit und Umwelt Ingolst?dter Landstrasse 1 85764 Neuherberg, Germany http://mips.gsf.de/staff/pagel