英文:
Converting string column to datetime variable using as.POSIXct function in R
问题
我想将一个字符串列转换为日期时间变量。我尝试了不同的函数,但似乎没有一个有效。如果你们中的某人能帮助我,我将不胜感激。
print(as.POSIXct(data$MTU, format = "%Y-%m-%d %H:%M:%S", tz = "Europe/Berlin"))
所以我希望从这个:
日期 |
---|
01.01.2022 00:00- 01.01.2022 00:15 |
实现类似这样的结果:
日期 | 时间 |
---|---|
01.01.2022 | 00:00 - 00:15 |
... | ... |
英文:
I would like to convert a string column into a datetime variable. I tried it with different functions, but no one seems to work. I would be grateful if someone of you could help me.
print(as.POSIXct(data$MTU, format = "%Y-%m-%d %H:%M:%S", tz = "Europe/Berlin"))
so I want from this:
date |
---|
01.01.2022 00:00- 01.01.2022 00:15 |
to achieve something like this:
date | time |
---|---|
01.01.2022 | 00:00 - 00:15 |
... | ... |
答案1
得分: 1
这里有几个问题:
- 问题和示例代码的主题提到了 POSIXct,但示例输出不是 POSIXct。
- 问题中的代码使用的格式表明输入是年份后跟着一个减号,然后是月份等等,但这不是示例输入的格式。
- 我们无法确定输入是日、月、年还是月、日、年,因为示例中的输入是模棱两可的。我们将假定日在前。
- 字符串中有两个日期/时间,而不是一个
这里有一些可能有所帮助的代码。
这将每个日期/时间都转换为 POSIXct。我们假设每个日期/时间的第一个数字是日,第二个数字是月。请注意,如果时间是 00:00,则在输出中可能会被省略,但这只是打印方式。该值仍然是完整的 POSIXct。
x <- "01.01.2022 00:00- 01.01.2022 00:15" # 测试输入
as.POSIXct(x, format = "%d.%m.%Y %H:%M", tz = "Europe/Berlin") # 第一个
## [1] "2022-01-01 CET"
as.POSIXct(sub(".*-", "", x), format = "%d.%m.%Y %H:%M",
tz = "Europe/Berlin") # 第二个
## [1] "2022-01-01 00:15:00 CET"
这将日期作为 Date 类型给出
as.Date(x, format = "%d.%m.%Y") # 第一个日期
## [1] "2022-01-01"
as.Date(sub(".*-", "", x), format = "%d.%m.%Y") # 第二个日期
## [1] "2022-01-01"
这将两个日期作为字符串给出。我们用空字符串替换首次出现的空格及其后的所有内容。然后,我们用捕获的字符串替换所有内容,直到出现连字符,然后是一个捕获的字符串和空格,后面跟着任何内容。
sub(" .*", "", x) # 第一个日期作为字符串
## [1] "01.01.2022"
sub(".*- (.*) .*", "\", x) # 第二个日期作为字符串
## [1] "01.01.2022"
这将给出问题中显示的时间字符串。我们将每次出现的可选空格、10个非空格字符和额外的可选空格都替换为空字符串。
gsub(" *\\S{10} *", "", x)
## [1] "00:00-00:15"
英文:
There are several problems:
- the subject of the question and sample code refer to POSIXct but the sample output is not POSIXct
- the format used in the code in the question says the input is a year followed by a minus followed by a month, etc. but that is not the format of the sample input
- we can't tell whether the input is day, month, year or whether it is month, day, year since the input shown is ambiguous. We will assume day comes first.
- there are two date/times in the string, not one
Here is some code which may help.
This gives each of the two date/times as POSIXct. We have assumed that the first number in each is the day and the second is the month. Note that the time may be suppressed on output if the time is 00:00 but that is just how it is printed. The value is still full POSIXct.
x <- "01.01.2022 00:00- 01.01.2022 00:15" # test input
as.POSIXct(x, format = "%d.%m.%Y %H:%M", tz = "Europe/Berlin") # 1st
## [1] "2022-01-01 CET"
as.POSIXct(sub(".*-", "", x), format = "%d.%m.%Y %H:%M",
tz = "Europe/Berlin") # 2nd
## [1] "2022-01-01 00:15:00 CET"
This gives the date as Date class
as.Date(x, format = "%d.%m.%Y") # 1st date
## [1] "2022-01-01"
as.Date(sub(".*-", "", x), format = "%d.%m.%Y") # 2nd date
## [1] "2022-01-01"
This gives the two dates date as strings. We replace the first space encountered plus everything following with the empty string. We then replace everything up to - followed by dash, a captured string and space followed by anything with the captured string.
sub(" .*", "", x) # first date as string
## [1] "01.01.2022"
sub(".*- (.*) .*", "\", x) # second date as string
## [1] "01.01.2022"
This gives the time string shown in the question. We replace each occurrence of optional spaces, 10 non-space characters and additional optional spaces with the empty string.
gsub(" *\\S{10} *", "", x)
## [1] "00:00-00:15"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论