如何从 12:00:00 AM : 11:00:00 PM 中删除列 R 中每个值的“:00”

How to delete ":00" from 12:00:00 AM : 11:00:00 PM for each value in a column R

我正在尝试更改此值

> hourlyIntensities_mergedcleannew
 [1] "12:00:00 AM" "1:00:00 AM"  "2:00:00 AM"  "3:00:00 AM"  "4:00:00 AM"  "5:00:00 AM"  "6:00:00 AM" 
 [8] "7:00:00 AM"  "8:00:00 AM"  "9:00:00 AM"  "10:00:00 AM" "11:00:00 AM" "12:00:00 PM" "1:00:00 PM" 
[15] "2:00:00 PM"  "3:00:00 PM"  "4:00:00 PM"  "5:00:00 PM"  "6:00:00 PM"  "7:00:00 PM"  "8:00:00 PM" 
[22] "9:00:00 PM"  "10:00:00 PM" "11:00:00 PM"

至此

[1] "12:00 AM" "1:00AM"  "2:00AM"  "3:00 AM" .. etc

使用gsub,

x <- c("12:00:00 AM", "1:00:00 AM", "2:00:00 AM", "3:00:00 AM", "4:00:00 AM", "5:00:00 AM", "6:00:00 AM")
gsub(':00 '," ",x)

输出将是

[1] "12:00 AM" "1:00 AM"  "2:00 AM"  "3:00 AM"  "4:00 AM"  "5:00 AM" 
[7] "6:00 AM"

这是另一个格式化为时间的选项:

df$time <- format(strptime(df$time, "%I:%M:%S %p"), '%I:%M %p')

输出

      time
1 12:00 AM
2 01:00 AM
3 02:00 AM
4 03:00 AM
5 04:00 AM
6 05:00 AM
7 06:00 AM
8 01:00 PM
9 09:00 PM

数据

df <- structure(list(time = c("12:00:00 AM", "1:00:00 AM", "2:00:00 AM", 
"3:00:00 AM", "4:00:00 AM", "5:00:00 AM", "6:00:00 AM", "1:00:00 PM", 
"9:00:00 PM")), class = "data.frame", row.names = c(NA, -9L))