将字符串列转换为日期时间变量,使用R中的as.POSIXct函数。

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

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 &lt;- &quot;01.01.2022 00:00- 01.01.2022 00:15&quot; # test input

as.POSIXct(x, format = &quot;%d.%m.%Y %H:%M&quot;, tz = &quot;Europe/Berlin&quot;) # 1st
## [1] &quot;2022-01-01 CET&quot;

as.POSIXct(sub(&quot;.*-&quot;, &quot;&quot;, x), format = &quot;%d.%m.%Y %H:%M&quot;, 
  tz = &quot;Europe/Berlin&quot;) # 2nd
## [1] &quot;2022-01-01 00:15:00 CET&quot;

This gives the date as Date class

as.Date(x, format = &quot;%d.%m.%Y&quot;) # 1st date
## [1] &quot;2022-01-01&quot;

as.Date(sub(&quot;.*-&quot;, &quot;&quot;, x), format = &quot;%d.%m.%Y&quot;) # 2nd date
## [1] &quot;2022-01-01&quot;

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(&quot; .*&quot;, &quot;&quot;, x) # first date as string
## [1] &quot;01.01.2022&quot;

sub(&quot;.*- (.*) .*&quot;, &quot;\&quot;, x) # second date as string
## [1] &quot;01.01.2022&quot;

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(&quot; *\\S{10} *&quot;, &quot;&quot;, x)
## [1] &quot;00:00-00:15&quot;

huangapple
  • 本文由 发表于 2023年6月1日 15:39:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379663.html
匿名

发表评论

匿名网友

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

确定