[R] Method Guidance

Avi Gross @v|gro@@ @end|ng |rom ver|zon@net
Wed Jan 12 18:26:28 CET 2022


To be fair, Jim, when people stop and think carefully before sending a message to a forum like this, and then explain their situation and request carefully and in enough detail, often a solution comes to them and they can abort sending!
Yes, the question asked is a bit vague. Other than asking for more details, the answers would also tend to be vague, or completely mysterious.
Someone sent a pointer to a web page that may well supply ideas to use, but maybe not as the question remains unclear.
So here is my vague reply. You say you have data in what seems to be a data.frame that includes some events marked with a starting time and other related events with a starting/stopping time or something like that. 
You want in some way to line up two or more such related events so you can calculate something like time elapsed. I am not clear if these come in pairs or can be extended as in go from point A to B then C and so on. For some such things, a data.frame may not ultimately be the right data structure.
So first you need to specify how to match things up. Is there another common field like StudentID that can be used to find all records showing when they began doing A or B? If not, how do you recognize what B goes with what A? The difference matters in terms of how to approach the problem.
Once you show a small representative sample, maybe people can suggest approaches. I did not find your sample even slightly useful. Lots of 1 and 0 and maybe some wrapping lines.
The overall approaches for doing what I hope you are doing, vary. You can of course go through the data row by row and look forward and do a calculation and print results. Or, you might want to reshape your data, perhaps merging subsets of the data with other subsets, to create new data where the start and stop segments are in the same row in multiple columns. The latter might be useful for all kinds of analyses including making graphs.
Your example seems to suggest you have a variable that is focused on days so you have not shown some kind of date field. But do events always happen in subsequent days, like in a game of postal chess, or can it happen within a day, perhaps multiple times, like taking your dog for a walk? Can two events interweave or is there a guarantee that each event completes before the next round starts? Is your data in a specific time order so B always follows A?
Many questions like the above matter in understanding your purpose and goal and then seeing what algorithm will take the data you start with and do something. If organized in certain ways and with no errors, it may be trivial. If not, in some cases it simply is not doable. And in some cases, such as buying and selling shares of stock, you have interesting things like rules of what happens if you buy back shares within a month or so as to whether you adjust the basis and of course the length of time can determine capital gains rates. More complexity is whether you have to do some kind of averaging or FIFO or can designate which shares. Now this may have nothing to do with your application, but is an example of why some problems may better be treated differently than others.

-----Original Message-----
From: Jim Lemon <drjimlemon using gmail.com>
To: Jeff Newmiller <jdnewmil using dcn.davis.ca.us>
Cc: r-help mailing list <r-help using r-project.org>
Sent: Wed, Jan 12, 2022 1:44 am
Subject: Re: [R] Method Guidance

Hi Jeff,
A completely obscure question deserves a completely obscure answer:

jrdf<-read.table(text="Time  Event_A    Event_B  Lag_B
1          1        1        0
2          0        1        1
3          0        0        0
4          1        0        0
5          0        1        1
6          0        0        0
7          0        1        3
8          1        1        0
9          0        0        0
10        0        1        2",
header=TRUE,stringsAsFactors=FALSE)
plot(jrdf$Time-0.2,jrdf$Event_A,type="p",
 xlim=c(1,12),ylim=c(0.9,1.1),
 xlab="Day",ylab="",main="The As and the Bs",
 pch=c(" ","A")[jrdf$Event_A+1],yaxt="n")
points(jrdf$Time+jrdf$Lag_B+0.2,jrdf$Event_B,pch=c(" ","B")[jrdf$Event_B+1])

Jim

On Wed, Jan 12, 2022 at 1:33 PM Jeff Newmiller <jdnewmil using dcn.davis.ca.us> wrote:
>
> 1) Figure out how to post plain text please. What you saw is not what we see.
>
> 2) I see a "table" of input information but no specific expectation of what would come out of this hypothetical function.
>
> 3) Maybe you will find something relevant in this blog post: https://jdnewmil.github.io/blog/post/cumsum-and-diff-tricks/
>
> ps not sure what "grate so sour" was supposed to be.
>
> pps While loops are not necessarily evil.
>
> On January 11, 2022 4:56:20 PM PST, Jeff Reichman <reichmanj using sbcglobal.net> wrote:
> >R-Help Forum
> >
> >
> >
> >Looking for a little guidance. Have an issue were I'm trying to determine
> >the time between when Event A happened(In days) to when a subsequent Event B
> >happens. For Example at Time 1 Evat A happens and subsequently Event B
> >happens at the same day (0) and the next day (1) then Event A happens again
> >at time 4 and Event B happens the next day and 3 days later so on and so
> >forth. I gather there is no function that will do that so I suspect I will
> >need to grate so sour of do while loop?  Any suggestions?
> >
> >
> >
> >
> >
> >Time      Event_A              Event_B              Time_B
> >
> >1              1                              1
> >0
> >
> >2              0                              1
> >1
> >
> >3              0                              0
> >0
> >
> >4              1                              0
> >0
> >
> >5              0                              1
> >1
> >
> >6              0                              0
> >0
> >
> >7              0                              1
> >3
> >
> >8              1                              1
> >0
> >
> >9              0                              0
> >0
> >
> >10          0                              1                              2
> >
> >
> >
> >
> >Jeff Reichman
> >
> >
> >      [[alternative HTML version deleted]]
> >
> >______________________________________________
> >R-help using 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.
>
> --
> Sent from my phone. Please excuse my brevity.
>
> ______________________________________________
> R-help using 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.

______________________________________________
R-help using 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]]



More information about the R-help mailing list