[R] delete data row

Joshua Wiley jwiley.psych at gmail.com
Mon Oct 18 03:26:50 CEST 2010


I used the -which() construct initially to try to show "deleting"
cases.  I believe it hung around longer than it should have.  That
said, I have also had David's experience with NAs.  What about a
vectorized version of identical(TRUE, x)?  This avoids the which()
problem Bill pointed out, and the NA issue David mentioned.  Does it
introduce new problems?

x <- 1:10
y <- log(x-5)
VisTRUE <- Vectorize(isTRUE)
x[VisTRUE(y > -Inf)]

Josh


On Sun, Oct 17, 2010 at 4:38 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>
> On Oct 17, 2010, at 3:56 PM, William Dunlap wrote:
>
>>
>>> I had been thinking of:
>>>>
>>>> x <- c(1, (2^(0.5))^2 , 3, 5, (2^(0.5))^2 , 3, 1)
>>>> y <- 2
>>>> x[-which(zapsmall(x-y) == 0)]
>>>
>>> [1] 1 3 5 3 1
>>
>> Using which() to convert logicals into integer
>> subscripts is almost always unnecessary and often wrong.
>
> At one time I believed that too. However, in the situation where the test
> produces NA rather than a numeric value  when one is indexing in the first
> argument. I have had the unpleasant experience of pages if useless and
> frustrating to understand output because of this "feature".
>
> I learned to either use which() in the first argument to "[" or to use
> subset to avoid inadvertent "returns" from logical indexing.
>
>> x <- 1:10
>> y <- log(x-5)
> Warning message:
> In log(x - 5) : NaNs produced
>> x[y>-Inf]
> [1] NA NA NA NA  6  7  8  9 10
>
>> x[which(y>-Inf)]
> [1]  6  7  8  9 10
>
> If that test were used in a dataframe indexing, the entire line might come
> back as a "result".
>
>
>
>> In this case it fails when no x is close to y,
>> because integer(0) is the same thing as -integer(0):
>>
>>> x[-which(zapsmall(x-10) == 0)]
>>
>>  numeric(0)
>>
>> The whichless version, using logical subscripts,
>> works (in this case we want all of x):
>>
>>> x[zapsmall(x-10)!=0]
>>
>>  [1] 1 2 3 5 2 3 1
>
> Maybe the rule should be don't use the -which construction:
>
>> x <- c(1, (2^(0.5))^2 , 3, 5, (2^(0.5))^2 , 3, 1)
>> y <- 2
>> x[which(zapsmall(x-10) != 0)]
> [1] 1 2 3 5 2 3 1
>
> --
> David.
>>
>> When using logicals as subscripts, read the "["
>> as "such that".
>>
>> Bill Dunlap
>> Spotfire, TIBCO Software
>> wdunlap tibco.com
>
>

-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list