[R] Deleting rows based on identity variable

Rolf Turner r.turner at auckland.ac.nz
Thu Apr 2 23:26:18 CEST 2009


On 3/04/2009, at 9:30 AM, gina patel wrote:

> I have created this data frame to help illustrate my problem.
>
> id<-c(rep(1,5),rep(2,5),rep(3,5),rep(4,5),rep(5,5))
> x<-rep((seq(1:5)),5)
> y<-c(0, 0.1, 0.5, 0.4, 0.2, 0, 0.1, 0.5, 0.4, 0.12, 0, 0.1, 0.5,  
> 0.55, 0.2, 0, 0.1, 0.5, 0.3, 0.2, 0, 0.1, 0.6, 0.4, 0.1)
> d1<-cbind(id,x,y)
>
> I would like to delete all rows where id=4, however, when I tried  
> the command
>
>  d2=d1[-c(id==4),]
>
> and looked at d2 no data was removed.
>
> Thanks in advance, I appreciate any help.

Others have told you how to do what you want.  It would be useful
for you to understand why what you ***did*** didn't work.

Note that ``id==4'' yields a *logical* vector with entries TRUE and  
FALSE.

When an arithmetic operator is applied, as in -(id==4) [NOTE: THE ``c 
()'' wrapper
is TOTALLY UNNECESSARY HERE.] the vector is coerced to a numeric  
vector of
-1's (for TRUE) and 0's (for FALSE).  Thus you are asking d2 to be d1 
[i,] where
i is a vector of -1's and 0's.  The -1's say to throw away the ``1- 
th'' (first)
row of d1; the 0's say to pick out the 0-th row of d1 (and there is  
no 0-th row
so this doesn't do anything).  Thus you are in effect asking for d1 
[2:25,].

And that's what you get.  Look carefully --- it is not correct to say  
that when
you looked at d2 *no* data were removed; d2 is equal to d1 with its  
first row
removed.  I.e. your operation didn't remove what you wanted, but it  
removed
*something*.

	cheers,

		Rolf Turner

######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}




More information about the R-help mailing list