[R] Select only cases with negative values

Martin Maechler maechler at stat.math.ethz.ch
Mon Oct 20 09:58:56 CEST 2008


>>>>> "MJ" == Michael Just <mgjust at gmail.com>
>>>>>     on Sat, 18 Oct 2008 17:26:02 -0400 writes:

    MJ> Henrique,
    MJ> This code below from a R-help helper worked.

    >> dat <-data.frame(f)
    >> dat2<-dat[dat$d<0,]
    >> dat.lm <-lm(c~d, data=dat2)

But Henrique's advice, of using 'subset= d < 0' was really
better (in most respects I'd say). If it really did not work for you,
you have a strange (or broken?) R environment.

Again back to your example 

 c <- 1:10
 d <- c(-1,2,-3,-4,5,6,-7,8,-9,10)
 f <- cbind(c,d)
 dat <- data.frame(f)
 rm(c,d,f) ## << check to make sure 'c' or 'd' are not picked up from GlobalEnv [MM]
 ## 
 daN.lm <-lm(c ~ d, data = dat, subset = d < 0)
 summary(daN.lm)

Does this really not work for you?

Martin Maechler, ETH Zurich

    MJ> Thanks again,
    MJ> Michael Just

    MJ> On Sat, Oct 18, 2008 at 5:22 PM, Henrique Dallazuanna <wwwhsd at gmail.com> wrote:
    >> Works for me.
    >> 
    >> dat.lm <-lm(c~d, data = dat, subset=d < 0)
    >> 
    >> Please see if you have dat with d and c columns names.
    >>



More information about the R-help mailing list