4 Replies Latest reply on Jun 30, 2016 7:49 AM by wpurrer

    time based average in R + example


      1.) my question is how do i calculated time based average/min/max in R (per day)
      in PI/AF SDK this is very easy (but for some reason we like to reduce the load and do this calculations outside of PI :-) so i know how to get the data from pi ..per hour,...)


      2.) what did i do => its maybe a good example how to get data from PI to R


      I created a static function in C#

      public static Dictionary<string, double> GetRawData(string server, string tag, string start, string end)
                  Dictionary<string, double> vals = new Dictionary<string, double>();

                  PIServers servers = new PIServers();
                  PIServer MyPIServer = servers[server];

                  PIPoint MyTag = PIPoint.FindPIPoint(MyPIServer, tag);            
                  AFTime startTime = new AFTime(start);
                  AFTime endTime = new AFTime(end);
                  AFTimeRange timeRange = new AFTimeRange(startTime, endTime);            

                  AFValues myvalues = MyTag.RecordedValues(timeRange, OSIsoft.AF.Data.AFBoundaryType.Inside,"",true);
                  foreach (AFValue av in myvalues)
                          if (av.IsGood)
                              vals.Add(av.Timestamp.LocalTime.ToString(), Convert.ToDouble(av.Value));
                      catch (System.Exception ex)
                  return vals;


      and then accessed this function through R


        result<-clrCallStatic(xxxx.AFWrapper','GetRawData', server,tag,start,end)

        #pd=data.frame(time=strptime(names(result) ,"%d.%m.%Y %H:%M:%S"),value=unlist(result))
        pd=data.frame(time=strptime(names(result) ,"%d/%m/%Y %H:%M:%S"),value=unlist(result))

        return (pd)


      so how do i calculate in R a time based average (per day for example ?) does anyone have some idea ?

        • Re: time based average in R + example
          Marcos Vainer Loeff



          Have you taken a look at the White Paper - Integrating the PI System with R (2nd version released - Aug/2016) ? It explains how you can exchange values from PI and R within a .NET application.


          Please let me know if this helps you!

            • Re: time based average in R + example

              Hello, sorry but i don't understand how your answer helps me, as you can see in my example i already know how to access the data ( in the document is mostly about on how to get the R integrated into c# or java)
              what i have done is, directly accessing PI from R => and then it goes into the direction of advanced topics ...

                • Re: time based average in R + example

                  Hello Wolfgang,


                  I believe Marcos had the idea to do the average calculation in .NET

                  I have done some internet research and learned that R refers to average as mean but this is as expected an event weighted average. Researching further, one resources suggested to look for ?weighted.mean and others indicated the calculation would have to be implemented by coding in R.


                  Please accept our apologies for providing a kind of vague reply. Your question about how to build a time weighted average using PI Data in R can be reduced to how one can build a time weighted average in R. It's legitimate to seek if someone in the PI user community has a complete answer for your question. Please continue asking these kind of questions. Even they might not be related to PI, there could be suggestions useful for you and other users.


                  By the way and because you are retrieving PI Data through AF SDK, asking for calculated data may mean an additional roundtrip but there's a good chance PI Archive Subsystem has the data, required for the calculation, cached. Unless you found clear indications, I wouldn't be concerned about the load.

                  1 of 1 people found this helpful
                    • Re: time based average in R + example

                      Hello Gregor,


                      Thank you looking for "?weighted.mean" was a very good hint :-) i looked for a couple of things but this never appeared.

                      Regarding the issues, we have some calculation which access the data from various sites ... doing every calculation on the site just "increases the round trip size", we just try to get the raw data one time, and from this on do the rest of the calculations in R.



                      Ps: using R Markdown in combination with PI gives a very good and beautiful way to create reports!