Home > Mobile >  Calculate the mean time by row
Calculate the mean time by row

Time:03-06

I'm trying to calculate the average time of each rows in my dataframe and put the result in another daaframe in one column conting only the average time, but for each row I get the same result.

My dafaframe (df):

X      X2015.01.01    X2015.01.02       X2015.01.05        X2015.01.06
<time>      <time>         <time>             <time>           <time>
NA secs     NA secs    7.702222 hours   7.578889 hours  8.056667 hours  
NA secs     NA secs    6.682778 hours   6.664722 hours  6.567500 hours  
NA secs     NA secs    6.738056 hours   6.054722 hours  6.621667 hours  
NA secs     NA secs    9.473611 hours   NA hours        9.573889 hours  

What I have tried:

df_2 <- data.frame(matrix(nrow=nrow(df)))
for (i in 1:ncol(df)) {
  df_2$i<- mean(df[,i],na.rm = TRUE)
}

df_2

matrix.nrow...nrow.df.. mean
<lgl>                  <time>
NA                      7.708015 hours              
NA                      7.708015 hours          
NA                      7.708015 hours  

df

df <- data.frame(structure(list(
  X = 
    structure(c(NA_real_, NA_real_, NA_real_, NA_real_), 
              class = "difftime", 
              units = "secs"), 
  
    X2015.01.01 =
    structure(c(NA_real_, NA_real_, NA_real_, NA_real_), 
              class = "difftime",
              units = "secs"), 
    X2015.01.02 = 
    structure(c(7.20833333333333, 8.10916666666667, 
    6.6925, 7.33833333333333),
    class = "difftime", 
    units = "hours"), 
  
    X2015.01.03 = 
    structure(c(7.578889, 6.664722, 
    6.054722 , NA_real_),
    class = "difftime", 
    units = "hours"),

    X2015.01.04 = 
    structure(c(8.056667 , 6.567500 , 
    6.621667  , 9.573889 ),
    class = "difftime", 
    units = "hours")

)))

CodePudding user response:

Converting your columns to duration available within lubridate facilitates that calculation:

df %>%
    mutate(across(everything(), ~ as.duration(.x))) %>%
    mutate(totalDuration = rowSums(.[1:5], na.rm = TRUE),
           avgDurationSecs = totalDuration / 5,
           avgDurationHours = dhours(avgDurationSecs / (60*60))) %>%
    glimpse()

You will be also able to obtain further units by using dhours etc.

CodePudding user response:

I find out I could just simply convert the time to numeric and then use rowMeans

for (i in 1:ncol(df)){
  df[i]<-lapply(df[i], as.numeric)
}

df$avg<-rowMeans(df,na.rm=TRUE)

If you want to put it in another dataframe.

df_2 <- data.frame(df$avg)

  • Related