Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6729767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:19:46+00:00 2026-05-26T10:19:46+00:00

I’m kinda new to R and just started using it to plot some graphs.

  • 0

I’m kinda new to R and just started using it to plot some graphs.

I have this code:

times=integer(nrow(df));
for(i in 1:nrow(df)) {
  time=df[i+1,4]-df[i,4];
  times[i]<-time
}

There must be a more clever way to do this, without first initializing times, isn’t it?
I’m not sure, but what I’m searching for is something like:

times <- for(i in 1:nrow(df)) yield df[i+1,4]-df[i,4]

(I know this is not valid code :))
I hope this question isn’t asked already. I searched and didn’t find anything concrete on “yield” and initializing of arrays.

As requested….

Sample data in df:

7926 08:00:27:ed:f3:e5 MESSAGEHANDLER START 1.319242e+12
7927 08:00:27:ed:f3:e5 MESSAGEHANDLER   END 1.319242e+12
7928 08:00:27:ed:f3:e5 MESSAGEHANDLER START 1.319242e+12
7929 08:00:27:ed:f3:e5 MESSAGEHANDLER   END 1.319242e+12
7930 08:00:27:ed:f3:e5 MESSAGEHANDLER START 1.319242e+12
7931 08:00:27:ed:f3:e5 MESSAGEHANDLER   END 1.319242e+12
7932 08:00:27:ed:f3:e5 MESSAGEHANDLER START 1.319242e+12
7933 08:00:27:ed:f3:e5 MESSAGEHANDLER   END 1.319242e+12
7934 08:00:27:ed:f3:e5 MESSAGEHANDLER START 1.319242e+12
7935 08:00:27:ed:f3:e5 MESSAGEHANDLER   END 1.319242e+12
7936 08:00:27:ed:f3:e5 MESSAGEHANDLER START 1.319242e+12
7937 08:00:27:ed:f3:e5 MESSAGEHANDLER   END 1.319242e+12
7938 08:00:27:ed:f3:e5 MESSAGEHANDLER START 1.319242e+12
7939 08:00:27:ed:f3:e5 MESSAGEHANDLER   END 1.319242e+12

After my loop is times is:

[7921] 508 500 497 501 466 502 505 500 488 501 500 501 490 501 478 501 501 501
[7939]  NA

Ok, to get more concrete, what I really want to do is this:

times1=integer(nrow(df));for(i in 1:nrow(df)) { if (df[i,3] == "START") times1[i]<-df[i+1,4]-df[i,4]}
times2=integer(nrow(df));for(i in 1:nrow(df)) { if (df[i,3] == "END") times2[i]<-df[i+1,4]-df[i,4]}

Then the output is something like for times1:

[7921]   0 500   0 501   0 502   0 500   0 501   0 501   0 501   0 501   0 501
[7939]   0

But I need:

[3960]   500   501   502   500   501   501   501   501   501

In words:

I’m parsing measured data from a csv file, which lands in df as seeing above.
This is for “START” followed by “END”

The data in df describes that a packet was received when there is a “START” in df[,3] at a specific unixtime in miliseconds in df[,4].
Now I need to calculate the time that passed from receiving to sending (this is the time, my machine needs to analyze the RECEIVED PACKET and calculate a result to SEND it.)
So END in df[,3] means packet was sent successfully at unixtime df[,4].

The other case is “END” followed by “START”

This is the time that passed in between “my packet was sent” and a new one “was received”.

I add now a sample of a csv and my full code for reproduction:

#load csv in df!
df = read.csv("/tmp/measure.csv",FALSE)
absolute=integer(nrow(df));for(i in 1:nrow(df)) {time=df[i,4]-df[1,4];absolute[i]<-(time/1000)}
times=integer(nrow(df));for(i in 1:nrow(df)) {time=df[i+1,4]-df[i,4];times[i]<-time}
#plot(absolute,times)
plot(absolute,times,lty=1,pch=1,col="#11223399",type="l")
lines(absolute,array(mean(times,na.rm=1),nrow(df)),col="red")

Here my measure.csv:

08:00:27:ed:f3:e5,TMCMESSAGEHANDLER,END,1319238175202
08:00:27:ed:f3:e5,TMCMESSAGEHANDLER,START,1319238175690
08:00:27:ed:f3:e5,TMCMESSAGEHANDLER,END,1319238176195
08:00:27:ed:f3:e5,TMCMESSAGEHANDLER,START,1319238176665
08:00:27:ed:f3:e5,TMCMESSAGEHANDLER,END,1319238177167
08:00:27:ed:f3:e5,TMCMESSAGEHANDLER,START,1319238177669
08:00:27:ed:f3:e5,TMCMESSAGEHANDLER,END,1319238178172
08:00:27:ed:f3:e5,TMCMESSAGEHANDLER,START,1319238178639
08:00:27:ed:f3:e5,TMCMESSAGEHANDLER,END,1319238179139
08:00:27:ed:f3:e5,TMCMESSAGEHANDLER,START,1319238179658
08:00:27:ed:f3:e5,TMCMESSAGEHANDLER,END,1319238180161
08:00:27:ed:f3:e5,TMCMESSAGEHANDLER,START,1319238180654
08:00:27:ed:f3:e5,TMCMESSAGEHANDLER,END,1319238181154
08:00:27:ed:f3:e5,TMCMESSAGEHANDLER,START,1319238181669
08:00:27:ed:f3:e5,TMCMESSAGEHANDLER,END,1319238182170
08:00:27:ed:f3:e5,TMCMESSAGEHANDLER,START,1319238182629
08:00:27:ed:f3:e5,TMCMESSAGEHANDLER,END,1319238183130

I hope this makes it more clear.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:19:46+00:00Added an answer on May 26, 2026 at 10:19 am

    Hope I’m not too far off- why not avoid the loop altogether?:

        # generate some data sort of similar to yours:
        DF <- data.frame(pos4 = rep(c("START","END"),10),times=rep(0,20))
        DF$times[DF$pos4=="START"] <- 1:10
        DF$times[DF$pos4=="END"] <- DF$times[DF$pos4=="START"]+runif(10)
        DF
        DF
            pos4 times
        1  START  1.000000
        2    END  1.750459
        3  START  2.000000
        4    END  2.212599
        5  START  3.000000
        6    END  3.974809
        ....
    

    I’m assuming the the START and END times in your dataset are in order..

        (times <- DF$times[DF$pos4=="END"] - DF$times[DF$pos4=="START"]) 
        [1] 0.7504590 0.2125986 0.9748094 0.3313644 0.3448410 0.8677022 0.9534317
        [8] 0.1279304 0.6500212 0.1798664
    

    not sure about what kind of checks you need to do, since they weren’t in the for loop you posted in the question.

    —————–EDIT—————————

    to include from the comment below that appears to have gotten it right,
    this really was a question about indexing:
    where:

        DIFFS <- diff(DF$times)
    

    gives you all the differences, you just wanted to split this into two objects, one for even indices, another for odd indices:

        times1 <- DIFFS[seq(from=1,to=length(DIFFS),by=2)]
        times2 <- DIFFS[seq(from=2,to=length(DIFFS),by=2)]
    

    and unrelated, but also useful: you used ‘absolute’ and ‘df’ for the names of objects in your code, but these are also functions in R, so although it works, it’s better form to give them names that aren’t already taken. Glad you got what you were after!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.