按行计算平均时间
我试图计算数据帧中每行的平均时间,并将结果放入仅包含平均时间的一列中的另一个数据帧中,但对于每一行,我得到相同的结果。
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将您的列转换为
lubridate
中可用的duration
有助于进行计算:您还可以使用
dhours
等获得更多单位。Converting your columns to
duration
available withinlubridate
facilitates that calculation:You will be also able to obtain further units by using
dhours
etc.我发现我可以简单地将时间转换为数字,然后使用 rowMeans
如果您想将其放入另一个数据帧中。
I find out I could just simply convert the time to numeric and then use rowMeans
If you want to put it in another dataframe.