From: jim holtman <jholtman_at_gmail.com>

Date: Fri, 27 May 2011 07:54:32 -0400

Date: Fri, 27 May 2011 07:54:32 -0400

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? ______________________________________________ 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.Received on Fri 27 May 2011 - 11:56:58 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

Archive maintained by Robert King, hosted by
the discipline of
statistics at the
University of Newcastle,
Australia.

Archive generated by hypermail 2.2.0, at Fri 27 May 2011 - 12:00:10 GMT.

*
Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help.
Please read the posting
guide before posting to the list.
*