Skip to content

Selecting vector elements using other vectors

2 messages · 00alastair00, Charles C. Berry

#
Dear R-Help,

I have a data frame and a vector

df=data.frame(Letter=c("Z","Q","R","A","E","F"), Number=c(11,32,4,1,9,3))
v=c("C","Q","R","A","E")
deletions dictated by the vector, v, of letters.  I've succeeded in doing
this for a single deletion (for example Letter="Q") with the following

df$Number[df$Letter != v[2]]

but am struggling do more than one at a time.  For example, 

df$Number[df$Letter != v[2:5]] 

gives the error:   Warning messages: 1: Is.na(e1) | is.na(e2) : longer
object length is not a multiple of shorter object length 2: In.....  Perhaps
there is a much easier way to approach this...

Any help would be greatly appreciated!

Alastair
#
See

 	?is.element
 	?%in%

HTH,

Chuck
On Wed, 29 Apr 2009, 00alastair00 wrote:

            
Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901