[R] Subset command and the : operator

jim holtman jholtman at gmail.com
Fri May 27 13:54:32 CEST 2011


What you are probably looking for is the %in% operator:

with(mydata, sum(table(X1[X2 %in% 5:8])))


Read up on how operations are vectorized and how variables are
recycled if not long enough

> x <- 1:10
> x == 1:2  # compares first two fine
 [1]  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> x == 1:3  # same for the first 3
 [1]  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> x == 4:6  # this is 4 5 6 4 5 6 which just happens to compare with 4 5 6 in x
 [1] FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE
> x == 2:5  # notice that this fail
 [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> x %in% c(8,2,7,3)  # order not important
 [1] FALSE  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE


On Fri, May 27, 2011 at 7:10 AM, Chris Beeley <chris.beeley at gmail.com> wrote:
> Hello-
>
> I have some code that looks like this:
>
> with(mydatalocal, sum(table(Service[Time==5:8])))
>
> This is designed to add up the numbers of responses between the Time
> codes 5 to 8 (which are integers and refer to quarters). Service is
> just one of the variables, I'm just trying to count the number of
> responses so I picked any of the variables. However, there is
> something wrong, it returns far too low a number for the number of
> responses. Indeed, if I run this:
>
> with(mydatalocal, sum(table(Service[Time==5|Time==6|Time==7|Time==8])))
>
> I get 4 times as many responses.
>
> I've tried to recreate the problem with the following code:
>
> mydata=data.frame(matrix(c(rep(1, 10), rep(2, 10), rep(3, 10), seq(1,
> 10, 1), seq(11, 20, 1), seq(21, 30, 1)), ncol=2))
>
> with(mydata, sum(table(X1[X2==9:12])))
>
> with(mydata, sum(table(X1[X2==9|X2==10|X2==11|X2==12])))
>
> but to my immense frustration it actually seems to work fine there,
> the same number, 4, both times. However, it does generate the
> following error message:
>
> In X2 == 9:12 :
>  longer object length is not a multiple of shorter object length
>
> I know I can use X1[ Time < 9 & Time > 3] but I would like to know
> what is wrong with the 5:8 usage in case I put it somewhere else and
> don't notice the problem.
>
> Many thanks!
>
> Chris Beeley
> Institute of Mental Health
>
> ______________________________________________
> R-help at 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.
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?



More information about the R-help mailing list