[R] Selecting vector elements using other vectors

2009-04-29 Thread 00alastair00

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)

From df, I'd like to construct a subset of the field Number, with
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
-- 
View this message in context: 
http://www.nabble.com/Selecting-vector-elements-using-other-vectors-tp23296130p23296130.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Selecting vector elements using other vectors

2009-04-29 Thread Charles C. Berry


See

?is.element
?%in%

HTH,

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



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)


From df, I'd like to construct a subset of the field Number, with

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
--
View this message in context: 
http://www.nabble.com/Selecting-vector-elements-using-other-vectors-tp23296130p23296130.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



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

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.