The different lengths work because R recycles values whenever you try to do
a binary operation on things of different lengths: in essence, R copies 10
however many times needed to make something that has the right length for an
elementwise comparison with x.**

If you did something like

x != c(1,10)

R would make a recycled-copy that looks like 1,10,1,10,1,10, etc. and
compare that to x, in essence, checking half the values against 1 and half
against 10.

Similarly, you can use vectors of totally different lengths and R will
simply repeat the shorter one to the right length:

e.g.,

(1:10) > (1:3)

this plays out as

c(1,2,3,4,5,6,7,8,9,10) > c(1,2,3,1,2,3,1,2,3)

but gives you a warning message that the two vectors don't "fit" correctly.
This message never comes up when one side is only a scalar because it can
always fit exactly.

You can see a surprising example of this at work with

(-5:5) > (-2 : 7)

which returns all FALSE values except for the last term, when 5 gets
compared to a recycled -2

When R makes this comparison, it creates a vector of logicals (TRUE and
FALSE values) and then the any() command tells us if there is at least 1
TRUE, which signals to us to keep the row.

Michael

** I'm not actually sure if that's how the code is implemented for the
scalar case, but it's probably easiest to think of it this way to get the
intuition for larger cases.

PS -- Is your data guaranteed to be an integer? If you have floating point
data, it's good practice to use something more like

abs(x - 10) > 1e-8

rather than x != 0 in your code. If you need to use this formulation, just
put it inside the any() statement.

On Mon, Aug 22, 2011 at 1:43 PM, Changbin Du <changbind@gmail.com> wrote:

> HI, Michael,
>
> Sorry for my numb, I have one more question.
>
> When you use function(x){any (x != 10), here x is a vector, x!=10 will give
> a vector of logical value, right?
>
> If it is, how can vector be compared to a scale, 10 in this case?
>
> Thanks!
>
>
>
>
>
> On Mon, Aug 22, 2011 at 10:16 AM, R. Michael Weylandt <
> michael.weylandt@gmail.com> wrote:
>
>> This isn't the most beautiful code, but I think it should work for you:
>>
>> # Some sample data
>> M =
>> cbind(matrix(rnorm(10),ncol=2),matrix(sample(c(10,1),15,replace=T),ncol=3))
>> colnames(M) = c("Thing1","Thing2",paste("array",1:3,sep=""))
>>
>> colsToCheck = grepl("array",colnames(M)) # Isolate the "array" columns
>> rowsToKeep = apply(M[,colsToCheck],1,function(x){any (x != 10)})
>> # apply the test function row-wise to get a logical vector of which rows
>> to keep
>>
>> Answer = M[rowsToKeep,] # keep only those rows
>>
>> Hope this helps,
>>
>> Michael
>>
>>
>> On Mon, Aug 22, 2011 at 12:56 PM, Changbin Du <changbind@gmail.com>wrote:
>>
>>> HI, Michael,
>>>
>>> What I want to do is remove all the rows, for which array1, array2,
>>> ..array15 are all equal to 10.
>>>
>>> I want to keep all the rows at least one of the array variables are not
>>> equal to 10.
>>>
>>> sorry for the confusion.
>>>
>>>
>>>
>>>
>>>
>>> On Mon, Aug 22, 2011 at 9:52 AM, R. Michael Weylandt <
>>> michael.weylandt@gmail.com> wrote:
>>>
>>>> "I want to select the array columns that are not equal to 10." is
>>>> ambiguous to me.
>>>>
>>>> Just to clarify, do you want to simply drop the column named array10 or
>>>> do you want to check each column for having one/all 10's as values and drop
>>>> based on that test?
>>>>
>>>> Michael
>>>>
>>>> On Mon, Aug 22, 2011 at 12:35 PM, Changbin Du <changbind@gmail.com>wrote:
>>>>
>>>>> Dear R community,
>>>>>
>>>>> I have a data set like the following:
>>>>>
>>>>>  probe_name chr_id  position array1 array2 array3 array4 array5 array6
>>>>> array7
>>>>> 1    C-3AAAA     10  16566949     10     10     10     10     10     10
>>>>> 10
>>>>> 2    C-3AAAB     17  33478940     10     10     10     10     10     10
>>>>> 10
>>>>> 3    C-3AAAC      3 187369224     10     10     2     10     10     1
>>>>>     10
>>>>> 4    C-3AAAD      8  28375041     10     10     10     10     10     10
>>>>> 10
>>>>> 5    C-3AAAG     13  99134921     10     10     10     10     10     10
>>>>> 10
>>>>> 6    C-3AAAH     16  31565412     10     10     10     10     10     10
>>>>> 10
>>>>>  array8 array9 array10 array11 array12 array13 array14 array15
>>>>> 1     10     10      10      10      10      10      10      10
>>>>> 2     10     10      10      10      10      10      10      10
>>>>> 3     10     10      10      10      10      10      10      10
>>>>> 4     10     10      10      10      10      10      10      10
>>>>> 5     10     10      10      10      10      1      10      10
>>>>> 6     10     10      10      0      10      10      10      10
>>>>>
>>>>> I want to select the array columns that are not equal to 10.
>>>>>
>>>>> I tried the following codes:
>>>>>
>>>>> head(reduce.final<-final[which(final$array*!=10), ])     # it does not
>>>>> wok,
>>>>> do any one have a smart to do this?
>>>>>
>>>>> Thanks so much!
>>>>>
>>>>>
>>>>> --
>>>>> Sincerely,
>>>>> Changbin
>>>>> --
>>>>>
>>>>>        [[alternative HTML version deleted]]
>>>>>
>>>>> ______________________________________________
>>>>> 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.
>>>>>
>>>>
>>>>
>>>
>>>
>>> --
>>> Sincerely,
>>> Changbin
>>> --
>>>
>>>
>>
>
>
> --
> Sincerely,
> Changbin
> --
>
>
>

	[[alternative HTML version deleted]]

