R - 将秒数转换为小时和分钟

时间:2021-01-24 02:47:31

I have a data frame like this,

我有这样的数据框,

Time ColA Colb
123  A     B

I would like to convert this into a dataframe like this,

我想将其转换为这样的数据帧,

Hours Minutes ColA Colb
0      2       A    B

The value in the time column is in the form of Seconds. How to convert this in to hours and minutes?

时间列中的值采用秒的形式。如何将其转换为小时和分钟?

2 个解决方案

#1


3  

I reproduce a similar example using the following R code:

我使用以下R代码重现了一个类似的例子:

library("lubridate")
library("dplyr")

Data <- data.frame(Time = seq(from = 100, to = 1200, by = 100),
                   ColA = rnorm(n = 12, mean = 0, sd = 1),
                   ColB = rnorm(n = 12, mean = 10, sd = 1))

Which generates a DataFrame with three columns, as yours: Time, ColA and ColB. The result looks like this:

它生成一个包含三列的DataFrame,如您的:Time,ColA和ColB。结果如下:

   Time    ColA      ColB
1  100  0.3819418  9.793732
2  200 -0.6819604  9.809536
3  300 -1.5910664 10.491511
4  400  0.5091230  8.251863
5  500  1.4298513 10.939813

Based on that you can use lubridate and dplyr libraries the get the result you are looking for, as follows:

基于此,您可以使用lubridate和dplyr库获取您要查找的结果,如下所示:

Data %>%
    mutate(Hours = hour(seconds_to_period(Time)),
           Minutes = minute(seconds_to_period(Time))) %>%
    select(Hours, Minutes, ColA, ColB)

Which generates the following results:

这会产生以下结果:

     Hours Minutes   ColA      ColB
1      0       1  0.3819418  9.793732
2      0       3 -0.6819604  9.809536
3      0       5 -1.5910664 10.491511
4      0       6  0.5091230  8.251863
5      0       8  1.4298513 10.939813

The logic behind that code is the following:

该代码背后的逻辑如下:

First, you are converting the seconds into a Period (lubridate's object to represent time lapses) using seconds_to_period() function. The result of that looks like this "2H 23M 20S" Then, using hour() and minute() you can extract the units you need, in this case hours and minutes. Finally, you can select the variables you want to keep using select(). I hope it helps!

首先,您使用seconds_to_period()函数将秒转换为Period(lubridate的对象以表示时间间隔)。结果看起来像这个“2H 23M 20S”然后,使用小时()和分钟(),你可以提取你需要的单位,在这种情况下是小时和分钟。最后,您可以使用select()选择要保留的变量。我希望它有所帮助!

#2


1  

Without additional packages you could make use of %/%, i.e. integer division (similar to A. Suliman's solution in the comments):

没有额外的包你可以使用%/%,即整数除法(类似于A. Suliman在评论中的解决方案):

df$Hour <- df$Time %/% (60 * 60)
df$Minute <- df$Time %/% 60
df
#  Time ColA Colb Hour Minute
#1  123    A    B    0      2

data

df <- read.table(text = "Time ColA Colb
123  A     B", header = TRUE, stringsAsFactors = FALSE)

#1


3  

I reproduce a similar example using the following R code:

我使用以下R代码重现了一个类似的例子:

library("lubridate")
library("dplyr")

Data <- data.frame(Time = seq(from = 100, to = 1200, by = 100),
                   ColA = rnorm(n = 12, mean = 0, sd = 1),
                   ColB = rnorm(n = 12, mean = 10, sd = 1))

Which generates a DataFrame with three columns, as yours: Time, ColA and ColB. The result looks like this:

它生成一个包含三列的DataFrame,如您的:Time,ColA和ColB。结果如下:

   Time    ColA      ColB
1  100  0.3819418  9.793732
2  200 -0.6819604  9.809536
3  300 -1.5910664 10.491511
4  400  0.5091230  8.251863
5  500  1.4298513 10.939813

Based on that you can use lubridate and dplyr libraries the get the result you are looking for, as follows:

基于此,您可以使用lubridate和dplyr库获取您要查找的结果,如下所示:

Data %>%
    mutate(Hours = hour(seconds_to_period(Time)),
           Minutes = minute(seconds_to_period(Time))) %>%
    select(Hours, Minutes, ColA, ColB)

Which generates the following results:

这会产生以下结果:

     Hours Minutes   ColA      ColB
1      0       1  0.3819418  9.793732
2      0       3 -0.6819604  9.809536
3      0       5 -1.5910664 10.491511
4      0       6  0.5091230  8.251863
5      0       8  1.4298513 10.939813

The logic behind that code is the following:

该代码背后的逻辑如下:

First, you are converting the seconds into a Period (lubridate's object to represent time lapses) using seconds_to_period() function. The result of that looks like this "2H 23M 20S" Then, using hour() and minute() you can extract the units you need, in this case hours and minutes. Finally, you can select the variables you want to keep using select(). I hope it helps!

首先,您使用seconds_to_period()函数将秒转换为Period(lubridate的对象以表示时间间隔)。结果看起来像这个“2H 23M 20S”然后,使用小时()和分钟(),你可以提取你需要的单位,在这种情况下是小时和分钟。最后,您可以使用select()选择要保留的变量。我希望它有所帮助!

#2


1  

Without additional packages you could make use of %/%, i.e. integer division (similar to A. Suliman's solution in the comments):

没有额外的包你可以使用%/%,即整数除法(类似于A. Suliman在评论中的解决方案):

df$Hour <- df$Time %/% (60 * 60)
df$Minute <- df$Time %/% 60
df
#  Time ColA Colb Hour Minute
#1  123    A    B    0      2

data

df <- read.table(text = "Time ColA Colb
123  A     B", header = TRUE, stringsAsFactors = FALSE)