How to calculate mean of two timestamp columns in R?

Lidia

I have data frame in R, where two columns are datetimes (POSIX class). I need to calculate mean datetime by each row.

Here's some reproducible example:

a <- c(
 "2018-10-11 15:22:17",
 "2018-10-10 16:30:37",
 "2018-10-10 16:52:46", 
 "2018-10-10 16:58:33", 
 "2018-10-10 16:32:24")

b <- c(
  "2018-10-11 15:25:12", 
  "2018-10-10 16:30:39", 
  "2018-10-10 16:55:14", 
  "2018-10-10 16:58:53", 
  "2018-10-10 16:32:27")

a <- strptime(a, format = "%Y-%m-%d %H:%M:%S")
b <- strptime(b, format = "%Y-%m-%d %H:%M:%S")

f <- data.frame(a, b)

The results should be like that:

                    a                   b           time_mean
1 2018-10-11 15:22:17 2018-10-11 15:25:12 2018-10-11 15:23:44
2 2018-10-10 16:30:37 2018-10-10 16:30:39 2018-10-10 16:30:38
3 2018-10-10 16:52:46 2018-10-10 16:55:14 2018-10-10 16:54:00
4 2018-10-10 16:58:33 2018-10-10 16:58:53 2018-10-10 16:58:43
5 2018-10-10 16:32:24 2018-10-10 16:32:27 2018-10-10 16:32:25

I tried following:

apply(f, 1, function(x) mean)
apply(f, 1, function(x) mean(c(x[1], x[2])))
akrun

Instead of using apply (which can convert it to a matrix and then strip off the class attributes), use Map

f$time_mean <- do.call(c, Map(function(x, y) mean(c(x, y)), a, b))
f$time_mean
#[1] "2018-10-11 15:23:44 EDT" "2018-10-10 16:30:38 EDT" "2018-10-10 16:54:00 EDT" "2018-10-10 16:58:43 EDT"
#[5] "2018-10-10 16:32:25 EDT"

Or as it is from data.frame f

do.call(c, Map(function(x, y) mean(c(x, y)), f$a, f$b))

Also, another option is converting to numeric class with ?xtfrm (that also has POSIXlt method dispatch), do the rowMeans and convert to DateTime class as in @jay.sf's post

as.POSIXlt(rowMeans(sapply(f, xtfrm)), origin = "1970-01-01")
#[1] "2018-10-11 15:23:44 EDT" "2018-10-10 16:30:38 EDT" "2018-10-10 16:54:00 EDT" "2018-10-10 16:58:43 EDT"
#[5] "2018-10-10 16:32:25 EDT"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Python

How to group by two columns, calculate weighted mean, return DataFrame, in Python

From Dev

How to calculate mean of only few columns of a text file in R?

From Dev

Calculate the mean value using two columns in pandas

From Dev

Barplot the mean of two columns in R

From Dev

Calculate Mean of Multiply Columns with Condition in R

From Dev

Calculate the mean of a row excluding certain columns in R

From Dev

Running a for loop in r over columns to calculate mean

From Dev

How to calculate difference between two timestamp columns depending on value of third column?

From Dev

How to count and calculate percentages for two columns in an R data.frame?

From Dev

Calculate mean of dataset in R that satisfies two conditions

From Dev

R Loop to calculate mean for each of two variables

From Dev

How to calculate the mean of a specific value in columns in Python?

From Dev

How to calculate row mean from selected columns

From Dev

sum two columns, calculate max, min and mean value in MapReduce

From Dev

Python Pandas - Calculate mean using criteria from two columns

From Dev

Group when values in two columns are identical and calculate the mean

From Dev

How to calculate mean of columns if not all columns are present in all the files?

From Dev

Calculate difference based on two columns in R

From Dev

How to calculate outliers by columns in R?

From Dev

How to properly subtract two UNIX timestamp columns

From Dev

dplyr/data.table: How to calculate the mean for counts of observations per group for two vectors containing factors in R

From Dev

How do I calculate the mean of two different variables taking into account the values of latitude and longitude in R?

From Dev

How to grep columns matching a pattern and calculate the row means of those columns and add the mean values as a new column to the data frame in r?

From Dev

Calculate mean euclidean distance of multiple columns dataframe r

From Dev

in R, how to calculate mean of all column, by group?

From Dev

How to calculate mean points for elements in a List in R

From Dev

How to calculate mean for all pairwise combinations in R

From Dev

How To Calculate Mean In R (Data Structure)

From Dev

R:how to calculate poblational standard deviation and mean

Related Related

  1. 1

    How to group by two columns, calculate weighted mean, return DataFrame, in Python

  2. 2

    How to calculate mean of only few columns of a text file in R?

  3. 3

    Calculate the mean value using two columns in pandas

  4. 4

    Barplot the mean of two columns in R

  5. 5

    Calculate Mean of Multiply Columns with Condition in R

  6. 6

    Calculate the mean of a row excluding certain columns in R

  7. 7

    Running a for loop in r over columns to calculate mean

  8. 8

    How to calculate difference between two timestamp columns depending on value of third column?

  9. 9

    How to count and calculate percentages for two columns in an R data.frame?

  10. 10

    Calculate mean of dataset in R that satisfies two conditions

  11. 11

    R Loop to calculate mean for each of two variables

  12. 12

    How to calculate the mean of a specific value in columns in Python?

  13. 13

    How to calculate row mean from selected columns

  14. 14

    sum two columns, calculate max, min and mean value in MapReduce

  15. 15

    Python Pandas - Calculate mean using criteria from two columns

  16. 16

    Group when values in two columns are identical and calculate the mean

  17. 17

    How to calculate mean of columns if not all columns are present in all the files?

  18. 18

    Calculate difference based on two columns in R

  19. 19

    How to calculate outliers by columns in R?

  20. 20

    How to properly subtract two UNIX timestamp columns

  21. 21

    dplyr/data.table: How to calculate the mean for counts of observations per group for two vectors containing factors in R

  22. 22

    How do I calculate the mean of two different variables taking into account the values of latitude and longitude in R?

  23. 23

    How to grep columns matching a pattern and calculate the row means of those columns and add the mean values as a new column to the data frame in r?

  24. 24

    Calculate mean euclidean distance of multiple columns dataframe r

  25. 25

    in R, how to calculate mean of all column, by group?

  26. 26

    How to calculate mean points for elements in a List in R

  27. 27

    How to calculate mean for all pairwise combinations in R

  28. 28

    How To Calculate Mean In R (Data Structure)

  29. 29

    R:how to calculate poblational standard deviation and mean

HotTag

Archive