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

Time:04-22

I am attempting to change this value

> 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"

to this

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

CodePudding user response:

Using 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)

The output would be

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

CodePudding user response:

Here is another option formatting as time:

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

Output

      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

Data

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))
  • Related