在匹配表达式 R 的列中向字符串追加字符。

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

Append characters to strings in column which match expression R

问题

我有一个日期/时间列,格式不一致:

df <- data.frame(date.time=c("10-29-2022 09:46:40", "02-27-2023 22:53:53", "12-15-2022 02:54:03", "01-09-2023 14:44", "03-05-2023 14:58", "02-18-2023 19:46:35", "12-10-2022 16:50"))

我想在不包含秒的时间戳后面添加":00"(注意有些字符串比其他字符串短)。我可以使用gsub(pattern=)找到没有秒的字符串,但是当我尝试在末尾添加":00"时,我替换了整个字符串:

df %>%
  mutate_at(.vars = "date.time", 
            .funs = gsub,
            pattern = "^\\d\\d-\\d\\d-\\d\\d\\d\\d \\d\\d:\\d\\d$",
            replacement = "\\d\\d-\\d\\d-\\d\\d\\d\\d \\d\\d:\\d\\d:00")
英文:

I have a date/time column in an inconsistent format:

df &lt;- data.frame(date.time=c(&quot;10-29-2022 09:46:40&quot;, &quot;02-27-2023 22:53:53&quot;, &quot;12-15-2022 02:54:03&quot;, &quot;01-09-2023 14:44&quot;, &quot;03-05-2023 14:58&quot;, &quot;02-18-2023 19:46:35&quot;, &quot;12-10-2022 16:50&quot;))

I want to add ":00" to the time stamps that don't include seconds (notice some strings are shorter than others). I can find the strings without seconds using gsubs(pattern=) but when I try to add on ":00" to the end I'm replacing the whole string:

df %&gt;%
  mutate_at(.vars = &quot;date.time&quot;, 
            .funs = gsub,
            pattern = &quot;^\\d\\d-\\d\\d-\\d\\d\\d\\d \\d\\d:\\d\\d$&quot;,
            replacement = &quot;\\d\\d-\\d\\d-\\d\\d\\d\\d \\d\\d:\\d\\d:00&quot;)

答案1

得分: 1

在这种情况下,你可以使用简单的ifelse语句,结合ncharpaste0函数,而无需使用正则表达式:

df$date.time_new <- ifelse(nchar(df$date.time) < max(nchar(df$date.time)), 
                           paste0(df$date.time, ":00"), 
                           df$date.time)

输出结果:

            date.time       date.time_new
1 10-29-2022 09:46:40 10-29-2022 09:46:40
2 02-27-2023 22:53:53 02-27-2023 22:53:53
3 12-15-2022 02:54:03 12-15-2022 02:54:03
4    01-09-2023 14:44 01-09-2023 14:44:00
5    03-05-2023 14:58 03-05-2023 14:58:00
6 02-18-2023 19:46:35 02-18-2023 19:46:35
7    12-10-2022 16:50 12-10-2022 16:50:00

希望对你有帮助!

英文:

In this case, you can use a simple ifelse with nchar and paste0 with no need for regex:

df$date.time_new &lt;- ifelse(nchar(df$date.time) &lt; max(nchar(df$date.time)), 
                           paste0(df$date.time, &quot;:00&quot;), 
                           df$date.time)

Output:

            date.time       date.time_new
1 10-29-2022 09:46:40 10-29-2022 09:46:40
2 02-27-2023 22:53:53 02-27-2023 22:53:53
3 12-15-2022 02:54:03 12-15-2022 02:54:03
4    01-09-2023 14:44 01-09-2023 14:44:00
5    03-05-2023 14:58 03-05-2023 14:58:00
6 02-18-2023 19:46:35 02-18-2023 19:46:35
7    12-10-2022 16:50 12-10-2022 16:50:00

huangapple
  • 本文由 发表于 2023年8月8日 23:13:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76860917.html
匿名

发表评论

匿名网友

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

确定