Adding the values of second column based on date and time of first column

USAL

I have a data frame with 2 variables. the first column "X" represents date and time with format dd/mm/yyyy hh:mm, the values in the second column "Y" are the electricity meter reading which are taken each after 5 minutes. Now I want to add the values of each half an hour. For instance

X                Y  
13/12/2014 12:00 1   
13/12/2014 12:05 2  
13/12/2014 12:10 1  
13/12/2014 12:15 2  
13/12/2014 12:20 2  
13/12/2014 12:25 1

At the end i want to present a result as:

13/12/2014 12:00 9  
13/12/2014 12:30 12  

and so on...

David Arenburg

Here's an alternative approach which actually takes X in count (as per OP comment).

First, we will make sure X is of proper POSIXct format so we could manipulate it correctly (I'm using the data.table package here for convenience)

library(data.table)
setDT(df)[, X := as.POSIXct(X, format = "%d/%m/%Y %R")]

Then, we will aggregate per cumulative minutes instances of 00 or 30 within X while summing Y and extracting the first value of X per each group. I've made a more complicated data set in order illustrate more complicated scenarios (see below)

df[order(X), .(X = X[1L], Y = sum(Y)), by = cumsum(format(X, "%M") %in% c("00", "30"))]
#    cumsum                   X Y
# 1:      0 2014-12-13 12:10:00 6
# 2:      1 2014-12-13 12:30:00 6
# 3:      2 2014-12-13 13:00:00 3

Data

df <- read.table(text = "X Y  
'13/12/2014 12:10' 1  
'13/12/2014 12:15' 2  
'13/12/2014 12:20' 2  
'13/12/2014 12:25' 1
'13/12/2014 12:30' 1
'13/12/2014 12:35' 1
'13/12/2014 12:40' 1
'13/12/2014 12:45' 1
'13/12/2014 12:50' 1
'13/12/2014 12:55' 1
'13/12/2014 13:00' 1
'13/12/2014 13:05' 1
'13/12/2014 13:10' 1", header = TRUE)

Some explanations

  • The by expression:
    • format(X, "%M") gets the minutes out of X (see ?strptime)
    • Next step is check if they match 00 or 30 (using %in%)
    • cumsum separates these matched values into separate groups which we aggregate by by putting this expression into the by statement (see ?data.table)
  • The jth epression
    • (X = X[1L], Y = sum(Y)) is simply getting the first value of X per each group and the sum of Y per each group.
  • The ith expression
    • I've added order(X) in order to make sure the data set is properly ordered by date (one of the main reasons I've converted X to proper POSIXct format)

For a better understanding on how data.table works, see some tutorials here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Adding the values of second column based on date and time of first column

From Dev

replace values in second column of a file based on matching values of first column

From Dev

Adding column based on matching of second column

From Dev

Fill in missing date values and populate second column based on previous row

From Dev

Adding all the values of the column based on column Header

From Dev

Adding column data based on column values?

From Dev

select column values based on other column date

From Dev

select column values based on other column date

From Dev

R create a column based on duplicate values of one column, and a second column

From Dev

Merging second column values for unique first column values

From Dev

selecting specific lines based on second column values

From Dev

Sum second column elements based on first column elements - MATLAB

From Dev

Subtract second column from two matrices based on first column

From Dev

Compare first column of 2 files based on the second column using the delimiter ;

From Dev

Bash - adding values in row based on column

From Dev

Adding values for missing rows based on a column value

From Dev

Adding a column of factors based on other categorical values

From Dev

Count values in first column where second column occurrences are the same in UNIX

From Dev

Find the smallest numbers in the second column corresponding to index values in first column

From Dev

Sum up values in first column for all identical strings in the second column

From Dev

Incrementally update date field column based on order of a second column

From Dev

SQL Subtract Column Values Based on Second Column Value with Group By Statement

From Dev

adding a column to Pandas dataframe based on adjacent values of existing column

From Dev

Adding values in column based on another column of separate table sql

From Dev

Merge multiple columns based on the first column values

From Dev

Dynamically calculate column values based on Date

From Dev

Reading a string column in xlsx with date and time values

From Dev

Reading a string column in xlsx with date and time values

From Dev

Sum of column values of dataframes with date time index

Related Related

  1. 1

    Adding the values of second column based on date and time of first column

  2. 2

    replace values in second column of a file based on matching values of first column

  3. 3

    Adding column based on matching of second column

  4. 4

    Fill in missing date values and populate second column based on previous row

  5. 5

    Adding all the values of the column based on column Header

  6. 6

    Adding column data based on column values?

  7. 7

    select column values based on other column date

  8. 8

    select column values based on other column date

  9. 9

    R create a column based on duplicate values of one column, and a second column

  10. 10

    Merging second column values for unique first column values

  11. 11

    selecting specific lines based on second column values

  12. 12

    Sum second column elements based on first column elements - MATLAB

  13. 13

    Subtract second column from two matrices based on first column

  14. 14

    Compare first column of 2 files based on the second column using the delimiter ;

  15. 15

    Bash - adding values in row based on column

  16. 16

    Adding values for missing rows based on a column value

  17. 17

    Adding a column of factors based on other categorical values

  18. 18

    Count values in first column where second column occurrences are the same in UNIX

  19. 19

    Find the smallest numbers in the second column corresponding to index values in first column

  20. 20

    Sum up values in first column for all identical strings in the second column

  21. 21

    Incrementally update date field column based on order of a second column

  22. 22

    SQL Subtract Column Values Based on Second Column Value with Group By Statement

  23. 23

    adding a column to Pandas dataframe based on adjacent values of existing column

  24. 24

    Adding values in column based on another column of separate table sql

  25. 25

    Merge multiple columns based on the first column values

  26. 26

    Dynamically calculate column values based on Date

  27. 27

    Reading a string column in xlsx with date and time values

  28. 28

    Reading a string column in xlsx with date and time values

  29. 29

    Sum of column values of dataframes with date time index

HotTag

Archive