将日期和时间分开在R中

huangapple go评论75阅读模式
英文:

Separate date and time in R

问题

我有包含日期和时间的数据集,数据类型为 "character",位于 "valid" 列中。我想要将这一列拆分为两列,一列是日期,一列是时间,但值应该保持不变。

这是从数据的前10行获取的第一个10个输出:

> head(data, 10)
   station            valid speed
1     PHNL 2021-01-01 00:00 14.95
2     PHNL 2021-01-01 00:05 21.85
3     PHNL 2021-01-01 00:10 17.25
4     PHNL 2021-01-01 00:15 19.55
5     PHNL 2021-01-01 00:20 16.10
6     PHNL 2021-01-01 00:25 17.25
7     PHNL 2021-01-01 00:30 12.65
8     PHNL 2021-01-01 00:35 17.25
9     PHNL 2021-01-01 00:40 18.40
10    PHNL 2021-01-01 00:45 17.25

我使用了 as.POSIXct,并且日期的结果与我预期的一样,但时间列仅显示为 "00:00",这意味着时间未在原始列中更新。我该如何解决这个问题?请帮忙,提前感谢。

data$date = as.Date(data$valid) 
data$time = format(as.POSIXct(data$valid), format="%H:%M")

结果:

  station            valid speed       date  time
1    PHNL 2021-01-01 00:00 14.95 2021-01-01 00:00
2    PHNL 2021-01-01 00:05 21.85 2021-01-01 00:05
3    PHNL 2021-01-01 00:10 17.25 2021-01-01 00:10
4    PHNL 2021-01-01 00:15 19.55 2021-01-01 00:15
5    PHNL 2021-01-01 00:20 16.10 2021-01-01 00:20
6    PHNL 2021-01-01 00:25 17.25 2021-01-01 00:25

时间现在已经正确更新为原始列中的值。

英文:

I have the dataset with the column consisting date and time as "character" class (the column "valid". I want to separate this column into two columns, one is for date and one is for time. But the values should be remained (not change).
This is the first 10 output from the data obtained from head(data, 10)

> head(data, 10)
   station            valid speed
1     PHNL 2021-01-01 00:00 14.95
2     PHNL 2021-01-01 00:05 21.85
3     PHNL 2021-01-01 00:10 17.25
4     PHNL 2021-01-01 00:15 19.55
5     PHNL 2021-01-01 00:20 16.10
6     PHNL 2021-01-01 00:25 17.25
7     PHNL 2021-01-01 00:30 12.65
8     PHNL 2021-01-01 00:35 17.25
9     PHNL 2021-01-01 00:40 18.40
10    PHNL 2021-01-01 00:45 17.25

I use as.POSIXct and I got the results as I expected for date, but the time column is just 00:00, it means the time is not updated as in the original column. How should I solve this problem? Kindly help and thank you in advance.

data$date=as.Date(data$valid) 
data$time=format(as.POSIXct(data$valid),
                 format="%H:%M")

Result

  station            valid speed       date  time
1    PHNL 2021-01-01 00:00 14.95 2021-01-01 00:00
2    PHNL 2021-01-01 00:05 21.85 2021-01-01 00:00
3    PHNL 2021-01-01 00:10 17.25 2021-01-01 00:00
4    PHNL 2021-01-01 00:15 19.55 2021-01-01 00:00
5    PHNL 2021-01-01 00:20 16.10 2021-01-01 00:00
6    PHNL 2021-01-01 00:25 17.25 2021-01-01 00:00

答案1

得分: 0

这是一种实现的方式:

# 加载库
pacman::p_load(tidyverse, hms)

# 创建一个 tibble,以便查看列的类型
read.table(text =
"    station            valid speed
1     PHNL '2021-01-01 00:00' 14.95
2     PHNL '2021-01-01 00:05' 21.85
3     PHNL '2021-01-01 00:10' 17.25
4     PHNL '2021-01-01 00:15' 19.55
5     PHNL '2021-01-01 00:20' 16.10
6     PHNL '2021-01-01 00:25' 17.25
7     PHNL '2021-01-01 00:30' 12.65
8     PHNL '2021-01-01 00:35' 17.25
9     PHNL '2021-01-01 00:40' 18.40
10    PHNL '2021-01-01 00:45' 17.25", header = TRUE) |> as_tibble() -> df

mutate(df, valid = as.POSIXct(valid, format = "%Y-%m-%d %H:%M"),
       date = as.Date(valid), 
       time = format(valid, "%H:%M"))

# 或者
mutate(df, valid = as_datetime(valid, format = "%Y-%m-%d %H:%M"),
       date = as_date(valid), 
       time = as_hms(format(valid, "%H:%M:%S")))

输出:

# A tibble: 10 × 5
   station valid               speed date       time  
   <chr>   <dttm>              <dbl> <date>     <time>
 1 PHNL    2021-01-01 00:00:00  15.0 2021-01-01 00'00"
 2 PHNL    2021-01-01 00:05:00  21.8 2021-01-01 05'00"
 3 PHNL    2021-01-01 00:10:00  17.2 2021-01-01 10'00"
 4 PHNL    2021-01-01 00:15:00  19.6 2021-01-01 15'00"
 5 PHNL    2021-01-01 00:20:00  16.1 2021-01-01 20'00"
 6 PHNL    2021-01-01 00:25:00  17.2 2021-01-01 25'00"
 7 PHNL    2021-01-01 00:30:00  12.6 2021-01-01 30'00"
 8 PHNL    2021-01-01 00:35:00  17.2 2021-01-01 35'00"
 9 PHNL    2021-01-01 00:40:00  18.4 2021-01-01 40'00"
10 PHNL    2021-01-01 00:45:00  17.2 2021-01-01 45'00"
英文:

Here's a couple of ways of doing it:

# load libraries
pacman::p_load(tidyverse, hms)

# create it as a tibble, so we can see the column types
read.table(text =
&quot;    station            valid speed
1     PHNL &#39;2021-01-01 00:00&#39; 14.95
2     PHNL &#39;2021-01-01 00:05&#39; 21.85
3     PHNL &#39;2021-01-01 00:10&#39; 17.25
4     PHNL &#39;2021-01-01 00:15&#39; 19.55
5     PHNL &#39;2021-01-01 00:20&#39; 16.10
6     PHNL &#39;2021-01-01 00:25&#39; 17.25
7     PHNL &#39;2021-01-01 00:30&#39; 12.65
8     PHNL &#39;2021-01-01 00:35&#39; 17.25
9     PHNL &#39;2021-01-01 00:40&#39; 18.40
10    PHNL &#39;2021-01-01 00:45&#39; 17.25&quot;, header = TRUE) |&gt; as_tibble() -&gt; df

mutate(df, valid = as.POSIXct(valid, format = &quot;%Y-%m-%d %H:%M&quot;),
           date = as.Date(valid), 
           time = format(valid, &quot;%H:%M&quot;))

# or
mutate(df, valid = as_datetime(valid, format = &quot;%Y-%m-%d %H:%M&quot;),
              date = as_date(valid), 
              time = as_hms(format(valid, &quot;%H:%M:%S&quot;)))

Output:

# A tibble: 10 &#215; 5
   station valid               speed date       time  
   &lt;chr&gt;   &lt;dttm&gt;              &lt;dbl&gt; &lt;date&gt;     &lt;time&gt;
 1 PHNL    2021-01-01 00:00:00  15.0 2021-01-01 00&#39;00&quot;
 2 PHNL    2021-01-01 00:05:00  21.8 2021-01-01 05&#39;00&quot;
 3 PHNL    2021-01-01 00:10:00  17.2 2021-01-01 10&#39;00&quot;
 4 PHNL    2021-01-01 00:15:00  19.6 2021-01-01 15&#39;00&quot;
 5 PHNL    2021-01-01 00:20:00  16.1 2021-01-01 20&#39;00&quot;
 6 PHNL    2021-01-01 00:25:00  17.2 2021-01-01 25&#39;00&quot;
 7 PHNL    2021-01-01 00:30:00  12.6 2021-01-01 30&#39;00&quot;
 8 PHNL    2021-01-01 00:35:00  17.2 2021-01-01 35&#39;00&quot;
 9 PHNL    2021-01-01 00:40:00  18.4 2021-01-01 40&#39;00&quot;
10 PHNL    2021-01-01 00:45:00  17.2 2021-01-01 45&#39;00&quot;

Feel free to comment if you have any questions!

huangapple
  • 本文由 发表于 2023年7月13日 22:47:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680717.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定