[R] sample (randomly select) from successive days

Jeff Newmiller jdnewmil @ending from dcn@d@vi@@c@@u@
Fri Dec 7 15:41:35 CET 2018


myfirst <- sample( seq.int( nrow(myframe)-1 ), 1 )
mysample <- myframe[seq( myfirst, myfirst+1),]
mysample

On December 7, 2018 2:24:11 AM PST, Dagmar Cimiotti <dagmar.cimiotti using ftz-west.uni-kiel.de> wrote:
>Hi Jim and everyone else,
>
>Mhm, no this is not what I am looking for. I think in your way I would 
>randomly sample two values of day 1 and of day 2. But I want the 
>opposite: I want to randomly draw two successive (!) days and put those
>
>values in a new dataframe to continue working with them.
>
>In my real data I do have a huge time span and I want to draw 25 
>consecutive days. So maybe my example was a little misleading. And now 
>that I read it again my text was, too. Sorry about that!
>
>Good try though and I am very gratefull for your good will to help me 
>:-)   Would anyone give another try?
>
>Dagmar
>
>Am 07.12.2018 um 10:30 schrieb Jim Lemon:
>> Hi Dagmar,
>> This will probably involve creating a variable to differentiate the
>> two days in each data.frame:
>>
>> myframe$day<-as.Date(as.character(myframe$Timestamp),"%d.%m.%Y
>%H:%M:%S")
>> days<-unique(myframe$day)
>>
>> Then just sample the two subsets and concatenate them:
>>
>> myframe[c(sample(which(myframe$day==days[1]),2),
>>   sample(which(myframe$day==days[2]),2)),]
>>
>> Jim
>>
>>
>> On Fri, Dec 7, 2018 at 8:08 PM Dagmar Cimiotti
>> <dagmar.cimiotti using ftz-west.uni-kiel.de> wrote:
>>> Dear all,
>>>
>>> I have data from a time span like this:
>>>
>>> myframe <- data.frame (Timestamp=c("24.09.2012 09:00:00",
>"24.09.2012 10:00:00","25.09.2012 09:00:00",
>>>                                      "25.09.2012
>09:00:00","24.09.2012 09:00:00", "24.09.2012 10:00:00"),
>>>                           Event=c(50,60,30,40,42,54) )
>>> myframe
>>>
>>>
>>> I want to create a new dataframe which includes in this example the
>data from two successive days (in my real data I have a big time span
>and want data from 25 consecutive days). I understand that I can do a
>simple sample like this
>>>
>>> mysample <- myframe[sample(1:nrow(myframe), 4,replace=FALSE),]
>>> mysample
>>>
>>> But I need the data from consecutive days in my random sample. Can
>anyone help me with this?
>>>
>>>
>>> Many thanks in advance,
>>> Dagmar
>>>
>>> ______________________________________________
>>> 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.

-- 
Sent from my phone. Please excuse my brevity.



More information about the R-help mailing list