Hi, I got caught out by this behaviour in 1.8.0 and I wondered why this happens: I have a list of vectors and was using lapply and grep to remove matched elements that occur in only a subset of the elements of the list: locs <- lapply(locs, function(x){x[- grep("^x", x)]}) The problem is that where the grep finds no matches and hence returns a vector of length 0, all the elements of x were removed, rather than none. As a toy example: vect <- 1:10 vect[-5] index <- 5 vect[-index] index <- numeric() vect[-index] # I was expecting this to give all the elements of vect rather than an empty vector vect[index] # does the same thing Thanks, David
S Poetry, page 72. If there is nothing there, then R can't know that you want the nothing to be negative. Patrick Burns Burns Statistics patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User") David Orme wrote:> Hi, > > I got caught out by this behaviour in 1.8.0 and I wondered why this > happens: > > I have a list of vectors and was using lapply and grep to remove > matched elements that occur in only a subset of the elements of the list: > locs <- lapply(locs, function(x){x[- grep("^x", x)]}) > > The problem is that where the grep finds no matches and hence returns > a vector of length 0, all the elements of x were removed, rather than > none. As a toy example: > > vect <- 1:10 > vect[-5] > index <- 5 > vect[-index] > index <- numeric() > vect[-index] # I was expecting this to give all the elements of vect > rather than an empty vector > vect[index] # does the same thing > > > Thanks, > David > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > >
David - I had to try your example verbatim before I understood what is happening. index <- numeric() creates a vector with no entries. Therefore the subscript is neither positive or negative, rather it contains no numeric values, so the return contains no entries either. Works the same in R-1.7.1 (which I am still running). Not sure whether they will be equivalent to what your grep() construct is doing, but take a look at help("unique"), help(duplicated). I use those all the time. - tom blackwell - u michigna medical school - ann arbor - On Fri, 14 Nov 2003, David Orme wrote:> Hi, > > I got caught out by this behaviour in 1.8.0 and I wondered why this > happens: > > I have a list of vectors and was using lapply and grep to remove > matched elements that occur in only a subset of the elements of the > list: > locs <- lapply(locs, function(x){x[- grep("^x", x)]}) > > The problem is that where the grep finds no matches and hence returns a > vector of length 0, all the elements of x were removed, rather than > none. As a toy example: > > vect <- 1:10 > vect[-5] > index <- 5 > vect[-index] > index <- numeric() > vect[-index] # I was expecting this to give all the elements of vect > rather than an empty vector > vect[index] # does the same thing > > Thanks, > David >
David Orme <d.orme at imperial.ac.uk> writes:> Hi, > > I got caught out by this behaviour in 1.8.0 and I wondered why this > happens: > > I have a list of vectors and was using lapply and grep to remove > matched elements that occur in only a subset of the elements of the > list: > locs <- lapply(locs, function(x){x[- grep("^x", x)]}) > > The problem is that where the grep finds no matches and hence returns > a vector of length 0, all the elements of x were removed, rather than > none. As a toy example: > > vect <- 1:10 > vect[-5] > index <- 5 > vect[-index] > index <- numeric() > vect[-index] # I was expecting this to give all the elements of vect > rather than an empty vector > vect[index] # does the same thing...which is the point: -index is just as empty as index and indexing by an empty vector gives you nothing. Probably the most convenient way out is ix <- 1:10 found <- numeric() vect(!(ix %in% found)) or maybe (I suspect more efficient) sel <- rep(T,10) found <- numeric() sel[found] <- FALSE x[sel] -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Just append the search string to the end of the target string, i.e. x <- c(x,"x") so that it always matches something:> my.list <- list(letters,LETTERS) > lapply(my.list, function(x){x <- c(x,"x"); x[- grep("^x", x)]})[[1]] [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" [20] "t" "u" "v" "w" "y" "z" [[2]] [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" [20] "T" "U" "V" "W" "X" "Y" "Z" --- Date: Fri, 14 Nov 2003 13:16:24 +0000 From: David Orme <d.orme at imperial.ac.uk> To: <r-help at stat.math.ethz.ch> Subject: [R] Vector indices and minus sign Hi, I got caught out by this behaviour in 1.8.0 and I wondered why this happens: I have a list of vectors and was using lapply and grep to remove matched elements that occur in only a subset of the elements of the list: locs <- lapply(locs, function(x){x[- grep("^x", x)]}) The problem is that where the grep finds no matches and hence returns a vector of length 0, all the elements of x were removed, rather than none. As a toy example: vect <- 1:10 vect[-5] index <- 5 vect[-index] index <- numeric() vect[-index] # I was expecting this to give all the elements of vect rather than an empty vector vect[index] # does the same thing Thanks, David
On Fri, 14 Nov 2003, David Orme wrote:> Hi, > > I got caught out by this behaviour in 1.8.0 and I wondered why this > happens: >Some anomaly of this sort is unavoidable because R allows positive or negative numeric indices. It can't distinguish between a vector of no positive numbers and a vector of no negative numbers. -thomas
> From: Thomas Lumley [mailto:tlumley at u.washington.edu] > > On Fri, 14 Nov 2003, David Orme wrote: > > > Hi, > > > > I got caught out by this behaviour in 1.8.0 and I wondered why this > > happens: > > > > Some anomaly of this sort is unavoidable because R allows > positive or negative numeric indices. It can't distinguish > between a vector of no positive numbers and a vector of no > negative numbers. > > -thomasI guess this can only work if "-" is treated specially in the subset operators, so that, e.g., x[-"Andy"] would also work. (This would be nice, IMHO.) [A bit OT: I remembered that among the list of bugs fixed in Splus 3.2 for Windows is -0 == 0 is F...] Andy
I guess what I suggested violates operator precedence rule. It would only work if somehow the subset operators can recognize the "-" first and treat whatever follows in a different way; i.e., to recognize that -numeric(0) is NOT the same as numeric(0). Oh well... Andy -----Original Message----- From: Patrick Burns [mailto:pburns@pburns.seanet.com] Sent: Friday, November 14, 2003 12:06 PM To: Liaw, Andy Cc: 'Thomas Lumley'; David Orme; r-help@stat.math.ethz.ch Subject: Re: [R] Vector indices and minus sign Liaw, Andy wrote: From: Thomas Lumley [mailto:tlumley@u.washington.edu <mailto:tlumley@u.washington.edu> ] On Fri, 14 Nov 2003, David Orme wrote: Hi, I got caught out by this behaviour in 1.8.0 and I wondered why this happens: Some anomaly of this sort is unavoidable because R allows positive or negative numeric indices. It can't distinguish between a vector of no positive numbers and a vector of no negative numbers. -thomas I guess this can only work if "-" is treated specially in the subset operators, so that, e.g., x[-"Andy"] would also work. (This would be nice, IMHO.) I agree that x[-c("Andy", "Pat")] would come in handy at times, but x[-numeric(0)] still can not work because the numbers that aren't there might be negative and the minus sign is to put them positive. Pat [A bit OT: I remembered that among the list of bugs fixed in Splus 3.2 for Windows is -0 == 0 is F...] Andy ______________________________________________ R-help@stat.math.ethz.ch <mailto:R-help@stat.math.ethz.ch> mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help <https://www.stat.math.ethz.ch/mailman/listinfo/r-help> [[alternative HTML version deleted]]