[R] how to find interval?
I can do this: 1:10 %in% c(3, 5, 6, 10) [1] FALSE FALSE TRUE FALSE TRUE TRUE FALSE FALSE FALSE TRUE but what I wish to get is: [1] 3 2 1 4 let me explain: 3 # [1:3] ends with TRUE, i.e. FALSE FALSE TRUE 2 # [4:5] ends with TRUE, i.e. FALSE TRUE 1 # [6:6] ends with TRUE, i.e. TRUE 4 # [7:10] ends with TRUE, i.e. FALSE FALSE FALSE TRUE That is, %in% gave me a serial whether or not the element is in a set, the length is equal to the former, i.e. 1:10 But I wish to get a serial of intevals of occurance of the element in the set, the length is equal to the latter i.e. c(3, 5, 6, 10) With ths task of finding the intervals, I found, with googling, a function called findInterval. I did read every line of that manual, and it seems to be for a completely different purpose. Kindly help the poor newbie:) __ 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.
Re: [R] how to find interval?
On 17-09-2013, at 12:15, gildororo...@mail-on.us wrote: I can do this: 1:10 %in% c(3, 5, 6, 10) [1] FALSE FALSE TRUE FALSE TRUE TRUE FALSE FALSE FALSE TRUE but what I wish to get is: [1] 3 2 1 4 let me explain: 3 # [1:3] ends with TRUE, i.e. FALSE FALSE TRUE 2 # [4:5] ends with TRUE, i.e. FALSE TRUE 1 # [6:6] ends with TRUE, i.e. TRUE 4 # [7:10] ends with TRUE, i.e. FALSE FALSE FALSE TRUE That is, %in% gave me a serial whether or not the element is in a set, the length is equal to the former, i.e. 1:10 But I wish to get a serial of intevals of occurance of the element in the set, the length is equal to the latter i.e. c(3, 5, 6, 10) One way is diff(c(0,which(1:10 %in% a))) [1] 3 2 1 4 Berend __ 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.
Re: [R] how to find interval?
I believe you want to use 'diff' and 'which' as in: diff(which(c(TRUE, 1:10 %in% c(3, 5, 6, 10) ))) [1] 3 2 1 4 Pat On 17/09/2013 11:15, gildororo...@mail-on.us wrote: I can do this: 1:10 %in% c(3, 5, 6, 10) [1] FALSE FALSE TRUE FALSE TRUE TRUE FALSE FALSE FALSE TRUE but what I wish to get is: [1] 3 2 1 4 let me explain: 3 # [1:3] ends with TRUE, i.e. FALSE FALSE TRUE 2 # [4:5] ends with TRUE, i.e. FALSE TRUE 1 # [6:6] ends with TRUE, i.e. TRUE 4 # [7:10] ends with TRUE, i.e. FALSE FALSE FALSE TRUE That is, %in% gave me a serial whether or not the element is in a set, the length is equal to the former, i.e. 1:10 But I wish to get a serial of intevals of occurance of the element in the set, the length is equal to the latter i.e. c(3, 5, 6, 10) With ths task of finding the intervals, I found, with googling, a function called findInterval. I did read every line of that manual, and it seems to be for a completely different purpose. Kindly help the poor newbie:) __ 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. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ 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.
Re: [R] how to find interval?
Hi I am not sure if my solution is general enough diff(c(0,which(1:10 %in% c(3, 5, 6, 10 [1] 3 2 1 4 Regards Petr -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of gildororo...@mail-on.us Sent: Tuesday, September 17, 2013 12:15 PM To: r-help@r-project.org Subject: [R] how to find interval? I can do this: 1:10 %in% c(3, 5, 6, 10) [1] FALSE FALSE TRUE FALSE TRUE TRUE FALSE FALSE FALSE TRUE but what I wish to get is: [1] 3 2 1 4 let me explain: 3 # [1:3] ends with TRUE, i.e. FALSE FALSE TRUE 2 # [4:5] ends with TRUE, i.e. FALSE TRUE 1 # [6:6] ends with TRUE, i.e. TRUE 4 # [7:10] ends with TRUE, i.e. FALSE FALSE FALSE TRUE That is, %in% gave me a serial whether or not the element is in a set, the length is equal to the former, i.e. 1:10 But I wish to get a serial of intevals of occurance of the element in the set, the length is equal to the latter i.e. c(3, 5, 6, 10) With ths task of finding the intervals, I found, with googling, a function called findInterval. I did read every line of that manual, and it seems to be for a completely different purpose. Kindly help the poor newbie:) __ 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. __ 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.
Re: [R] how to find interval?
Quoting PIKAL Petr petr.pi...@precheza.cz: diff(c(0,which(1:10 %in% c(3, 5, 6, 10 [1] 3 2 1 4 That solves the problem! Thanks. __ 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.
Re: [R] how to find interval?
On 17-09-2013, at 12:41, Berend Hasselman b...@xs4all.nl wrote: On 17-09-2013, at 12:15, gildororo...@mail-on.us wrote: I can do this: 1:10 %in% c(3, 5, 6, 10) [1] FALSE FALSE TRUE FALSE TRUE TRUE FALSE FALSE FALSE TRUE but what I wish to get is: [1] 3 2 1 4 let me explain: 3 # [1:3] ends with TRUE, i.e. FALSE FALSE TRUE 2 # [4:5] ends with TRUE, i.e. FALSE TRUE 1 # [6:6] ends with TRUE, i.e. TRUE 4 # [7:10] ends with TRUE, i.e. FALSE FALSE FALSE TRUE That is, %in% gave me a serial whether or not the element is in a set, the length is equal to the former, i.e. 1:10 But I wish to get a serial of intevals of occurance of the element in the set, the length is equal to the latter i.e. c(3, 5, 6, 10) One way is diff(c(0,which(1:10 %in% a))) [1] 3 2 1 4 and of course a equals c(3, 5, 6, 10) ! Berend __ 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.
Re: [R] how to find interval?
You could use findInterval to find which interval each element is in, then use table to count up how many are in each interval: table(findInterval( 1:10, c(3,5,6,10) + 10*.Machine$double.eps ) ) 0 1 2 3 3 2 1 4 On Tue, Sep 17, 2013 at 4:15 AM, gildororo...@mail-on.us wrote: I can do this: 1:10 %in% c(3, 5, 6, 10) [1] FALSE FALSE TRUE FALSE TRUE TRUE FALSE FALSE FALSE TRUE but what I wish to get is: [1] 3 2 1 4 let me explain: 3 # [1:3] ends with TRUE, i.e. FALSE FALSE TRUE 2 # [4:5] ends with TRUE, i.e. FALSE TRUE 1 # [6:6] ends with TRUE, i.e. TRUE 4 # [7:10] ends with TRUE, i.e. FALSE FALSE FALSE TRUE That is, %in% gave me a serial whether or not the element is in a set, the length is equal to the former, i.e. 1:10 But I wish to get a serial of intevals of occurance of the element in the set, the length is equal to the latter i.e. c(3, 5, 6, 10) With ths task of finding the intervals, I found, with googling, a function called findInterval. I did read every line of that manual, and it seems to be for a completely different purpose. Kindly help the poor newbie:) __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Gregory (Greg) L. Snow Ph.D. 538...@gmail.com [[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.