[R] Understand Rcode- subset

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Mon Aug 24 19:42:46 CEST 2015


The first id was ignored (d[2:length(id)]) and the last id in id.lag was set to FALSE. Because the values are in new positions even though the same number of id values are kept, a lag (shift) has been created that takes effect when id.lag is used as an index into transact.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

On August 24, 2015 9:29:41 AM PDT, Jhon Grey <butt_its_me at hotmail.com> wrote:
>Can you please explain elaborately! I understood that TRUE has been
>shifted ahead. But unfortunately did not understand the code- as how
>using logical vector it was shifted ? # lagged vector of id. TRUE is
>shifted one position aheadid.lag <- c(id[2:length(id)], FALSE)It
>shifted ahead twice using the code-id.lag <- c(id[3:length(id)],
>FALSE,FALSE)
>Thanking you again for your prompt response!
>> Date: Mon, 24 Aug 2015 17:55:28 +0200
>> Subject: Re: [R] Understand Rcode- subset
>> From: thierry.onkelinx at inbo.be
>> To: butt_its_me at hotmail.com
>> CC: r-help at r-project.org
>> 
>> # logical vector, TRUE when record_type equals 1
>> id <- transact$record_type == 1
>> # lagged vector of id. TRUE is shifted one position ahead
>> id.lag <- c(id[2:length(id)], FALSE)
>> sub <- transact[id.lag, ]
>> # have a look at the row numbers
>> transact[id, ]
>> sub
>> 
>> Best regards,
>> ir. Thierry Onkelinx
>> Instituut voor natuur- en bosonderzoek / Research Institute for
>Nature
>> and Forest
>> team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
>> Kliniekstraat 25
>> 1070 Anderlecht
>> Belgium
>> 
>> To call in the statistician after the experiment is done may be no
>> more than asking him to perform a post-mortem examination: he may be
>> able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher
>> The plural of anecdote is not data. ~ Roger Brinner
>> The combination of some data and an aching desire for an answer does
>> not ensure that a reasonable answer can be extracted from a given
>body
>> of data. ~ John Tukey
>> 
>> 
>> 2015-08-24 17:32 GMT+02:00 Jhon Grey <butt_its_me at hotmail.com>:
>> > Hi!
>> >
>> > Thanks for pointing out the mistake.
>> >
>> > I am resubmitting my question as follows:
>> >
>> > Hi!
>> > I am facing a problem in understanding the R-Code
>> > Suppose I have a transactional dataset named- "transact" with its
>values
>> > like following:
>> > customer_ID
>> >
><-c(10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000005,10000005,10000005,10000005,10000005,10000005,10000007,10000007,10000007,10000007,10000007,10000007,10000007,10000007,10000013,10000013)
>> > shopping_pt <-
>c(1,2,2,3,4,5,6,7,8,9,1,2,3,4,5,6,1,2,3,4,5,6,7,8,1,2)
>> > record_type <-
>c(0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0)
>> > transact <- data.frame(customer_ID, shopping_pt, record_type)
>> >
>> > How does the following code work
>> > id<-transact$record_type==1
>> > sub<-transact[c(id[2:length(id)],FALSE),]
>> > id<-c(id[3:length(id)],FALSE,FALSE)
>> > sub2<-transact[id,]
>> > Thanks in advance!
>> >
>> >> Date: Mon, 24 Aug 2015 15:25:09 +0200
>> >> Subject: Re: [R] Understand Rcode- subset
>> >> From: thierry.onkelinx at inbo.be
>> >> To: butt_its_me at hotmail.com
>> >> CC: r-help at r-project.org
>> >
>> >>
>> >> Posting in HTML mangles up your code, making it hard to read.
>Please
>> >> resend your question in plain text and make the code reproducible.
>See
>> >> http://adv-r.had.co.nz/Reproducibility.html for more details on
>that.
>> >> ir. Thierry Onkelinx
>> >> Instituut voor natuur- en bosonderzoek / Research Institute for
>Nature
>> >> and Forest
>> >> team Biometrie & Kwaliteitszorg / team Biometrics & Quality
>Assurance
>> >> Kliniekstraat 25
>> >> 1070 Anderlecht
>> >> Belgium
>> >>
>> >> To call in the statistician after the experiment is done may be no
>> >> more than asking him to perform a post-mortem examination: he may
>be
>> >> able to say what the experiment died of. ~ Sir Ronald Aylmer
>Fisher
>> >> The plural of anecdote is not data. ~ Roger Brinner
>> >> The combination of some data and an aching desire for an answer
>does
>> >> not ensure that a reasonable answer can be extracted from a given
>body
>> >> of data. ~ John Tukey
>> >>
>> >>
>> >> 2015-08-24 13:00 GMT+02:00 Jhon Grey <butt_its_me at hotmail.com>:
>> >> > Hi!
>> >> > I am facing a problem in understanding the R-Code
>> >> > Suppose I have a dataset named- transact with its values like
>following:
>> >> >
>> >> >
>> >> >
>> >> >
>> >> >
>> >> > customer_ID
>> >> > shopping_pt
>> >> > record_type
>> >> >
>> >> >
>> >> > 10000000
>> >> > 1
>> >> > 0
>> >> >
>> >> >
>> >> > 10000000
>> >> > 2
>> >> > 0
>> >> >
>> >> >
>> >> > 10000000
>> >> > 3
>> >> > 0
>> >> >
>> >> >
>> >> > 10000000
>> >> > 4
>> >> > 0
>> >> >
>> >> >
>> >> > 10000000
>> >> > 5
>> >> > 0
>> >> >
>> >> >
>> >> > 10000000
>> >> > 6
>> >> > 0
>> >> >
>> >> >
>> >> > 10000000
>> >> > 7
>> >> > 0
>> >> >
>> >> >
>> >> > 10000000
>> >> > 8
>> >> > 0
>> >> >
>> >> >
>> >> > 10000000
>> >> > 9
>> >> > 1
>> >> >
>> >> >
>> >> > 10000005
>> >> > 1
>> >> > 0
>> >> >
>> >> >
>> >> > 10000005
>> >> > 2
>> >> > 0
>> >> >
>> >> >
>> >> > 10000005
>> >> > 3
>> >> > 0
>> >> >
>> >> >
>> >> > 10000005
>> >> > 4
>> >> > 0
>> >> >
>> >> >
>> >> > 10000005
>> >> > 5
>> >> > 0
>> >> >
>> >> >
>> >> > 10000005
>> >> > 6
>> >> > 1
>> >> >
>> >> >
>> >> > 10000007
>> >> > 1
>> >> > 0
>> >> >
>> >> >
>> >> > 10000007
>> >> > 2
>> >> > 0
>> >> >
>> >> >
>> >> > 10000007
>> >> > 3
>> >> > 0
>> >> >
>> >> >
>> >> > 10000007
>> >> > 4
>> >> > 0
>> >> >
>> >> > How does the following code work and the results of sub and id-
>> >> >
>> >> >
>id<-transact$record_type==1sub<-train[c(id[2:length(id)],FALSE),]id<-c(id[3:length(id)],FALSE,FALSE)
>> >> > sub2<-train[id,]
>> >> > Thanks in advance!
>> >> >
>> >> > [[alternative HTML version deleted]]
>> >> >
>> >> > ______________________________________________
>> >> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more,
>see
>> >> > 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.
> 		 	   		  
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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.



More information about the R-help mailing list