0

I want to aggregate my data. The goal is to have for each time interval one point in a diagram. Therefore I have a data frame with 2 columns. The first columns is a timestamp. The second is a value. I want to evaluate each time period. That means: The values be added all together within the Time period for example 1 second. I don't know how to work with the aggregate function, because these function supports no time.

     0.000180   8
     0.000185   8
     0.000474   32
JEHA
  • 3
  • 2
  • Try looking at the `subset` function. Next time, read this link (https://stackoverflow.com/a/5963610/882102) on how to ask good questions. – MrGumble Sep 02 '19 at 12:24
  • @MrGumble The problem is that I can't using it with seconds and milliseconds. The second Column is a timestamp in seconds:milliseconds when a measurement comes in. The easy "subset" function can't subset a time range for example for one second. – JEHA Sep 02 '19 at 12:35

1 Answers1

1

It is not easy to tell from your question what you're specifically trying to do. Your data has no column headings, we do not know the data types, you did not include the error message, and you contradicted yourself between your original question and your comment (Is the first column the time stamp? Or is the second column the time stamp?

I'm trying to understand. Are you trying to:

  1. Split your original data.frame in to multiple data.frame's?
  2. View a specific sub-set of your data? Effectively, you want to filter your data?
  3. Group your data.frame in to specific increments of a set time-interval to then aggregate the results?

Assuming that you have named the variables on your dataframe as time and value, I've addressed these three examples below.

#Set Data
num <- 100
set.seed(4444)
tempdf <- data.frame(time = sample(seq(0.000180,0.000500,0.000005),num,TRUE), 
                     value = sample(1:100,num,TRUE))

#Example 1: Split your data in to multiple dataframes (using base functions)
temp1 <- tempdf[ tempdf$time>0.0003 , ]
temp2 <- tempdf[ tempdf$time>0.0003 & tempdf$time<0.0004 , ]

#Example 2: Filter your data (using dplyr::filter() function)
dplyr::filter(tempdf, time>0.0003 & time<0.0004)

#Example 3: Chain the funcions together using dplyr to group and summarise your data
library(dplyr)
tempdf %>% 
    mutate(group = floor(time*10000)/10000) %>% 
    group_by(group) %>% 
    summarise(avg = mean(value), 
              num = n())

I hope that helps?

chrimaho
  • 580
  • 4
  • 22