英文:
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 =
" 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"))
# or
mutate(df, valid = as_datetime(valid, format = "%Y-%m-%d %H:%M"),
date = as_date(valid),
time = as_hms(format(valid, "%H:%M:%S")))
Output:
# 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"
Feel free to comment if you have any questions!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论