简体   繁体   中英

getting average mean for each period in a ffdf object in R

I have an ffdf object called 'group1' that has a million rows of data that looks like this:

       Location                  DateandTime   Reading     Group
1             1          01/01/2012 00:00:00     0.8           1   
2             1          01/01/2012 00:30:00     0.4           1  
3             1          01/01/2012 01:00:00     0.7           1   
4             1          01/01/2012 01:30:00     0.2           1

I'm trying to get an average 'Reading' and standard deviation for each 'DateandTime' and create a new df to look something like this:

                      DateTime     mean     sd
1          01/01/2012 00:00:00     0.8     .2        
2          01/01/2012 00:30:00     0.5     .5        
3          01/01/2012 01:00:00     0.2     .3         
4          01/01/2012 01:30:00     0.8     .8

Or you can use dplyr package

    library(dplyr)
    group1.stat <- group1 %>% 
      select(DateandTime, Reading) %>% 
      group_by(DateandTime) %>% 
      summarise_each(funs(mean = mean(., na.rm = TRUE), sd = sd(., na.rm = TRUE)))
    group1.stat

This dplyr approach will also work:

library(dplyr)

newdf <- group1 %>% 
  group_by(DateandTime) %>%
  summarise(mean = mean(Reading), sd = sd(Reading))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM