[R] Conditionally Removing data from a vector
Philipp Pagel
p.pagel at wzw.tum.de
Fri Nov 21 23:15:21 CET 2008
> DEVS should equal 6 values the last being 620, like the data frame:
> CANDS DEVS
> > [1,] 100 120
> > [2,] 101 220
> > [3,] 102 320
> > [4,] 103 420
> > [5,] 104 520
> > [6,] 105 620
>
> And yes, my first question is do i need a "if" statement at all as the which
> command seems to do the same thing.
You don't - and you don't need which() either. R is pretty good
at indexing things in very conveniant ways. I'll assume we have
the data you gave as an example in a data.frame - I'll call 'df':
# create example data
df <- data.frame(cands=100:105, devs=c(120, 220, 320, 420, 520, 620))
threshold <- 400
# get all rows with devs above threshold
df[df$devs>threshold, ]
# alternative way giving the same result
subset(df, devs>threshold)
The above methods will work if devs and cands are separate
vectors, too:
# get two vectors of equal length
cands <- df$cands
devs <- df$devs
cands[devs>threshold]
# alternative
subset(cands, devs>threshold)
For details and more examples have a look at the "Introduction to
R" - especially section 2.7 "Index vectors; selecting and
modifying subsets of a data set".
cu
Philipp
--
Dr. Philipp Pagel
Lehrstuhl für Genomorientierte Bioinformatik
Technische Universität München
Wissenschaftszentrum Weihenstephan
85350 Freising, Germany
http://mips.gsf.de/staff/pagel
More information about the R-help
mailing list