按行计算平均时间

发布于 2025-01-11 17:10:56 字数 1721 浏览 0 评论 0原文

我试图计算数据帧中每行的平均时间,并将结果放入仅包含平均时间的一列中的另一个数据帧中,但对于每一行,我得到相同的结果。

我的 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  

我尝试过的:

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")

)))

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")

)))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

路弥 2025-01-18 17:10:56

将您的列转换为 lubridate 中可用的 duration 有助于进行计算:

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

您还可以使用 dhours 等获得更多单位。

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.

近箐 2025-01-18 17:10:56

我发现我可以简单地将时间转换为数字,然后使用 rowMeans

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

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

如果您想将其放入另一个数据帧中。

df_2 <- data.frame(df$avg)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文