AnsweredAssumed Answered

time based average in R + example

Question asked by wpurrer on Jun 28, 2016
Latest reply on Jun 30, 2016 by wpurrer

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)
            {
                try
                {
                    if (av.IsGood)
                        vals.Add(av.Timestamp.LocalTime.ToString(), Convert.ToDouble(av.Value));
                }
                catch (System.Exception ex)
                {
                    System.Console.WriteLine(ex.ToString());
                }
            }
            return vals;
        }

 

and then accessed this function through R

 

PD_GetRawData<-function(server,tag,start,end)
{
  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 ?

Outcomes