How to extract characters from strings of different lengths based on their position?
I'm going bald trying to figure it out. I am trying to extract characters from strings of varying length depending on position. For example, say I have a data frame with 8 rows and 1 column called "Time", The column has the following values in each row:
TIME
5
20
100
105
150
1050
2005
2010
I am trying to figure out how to extract the characters in these strings and into the following position based format. If you look, you can see that the last two characters are placed 00: here: 00 , and all other characters are placed here: 00: 00 . The result should be:
5 → 00:05:00 (if there is only 1 character)
20 → 00:20:00 (if there are 2 characters)
100 → 1:00:00 (if there are 3 characters)
105 → 1:05:00 (if there is 3 characters)
150 → 1:50:00 (if there are 3 characters)
1050 → 10:50:00 (if there are 4 characters)
2005 → 20:05:00 (if there are 4 characters)
2010 → 20:10:00 ( if there are 4 characters)
Basically the pseudocode would be something like strings from dataframe $ column1 extract the last two characters and put it 00: here: 00 - all other characters should go here: 00: 00
format( as.POSIXct(sprintf("%04.0f", TIME), format="%H%M"), format="%H:%M:%S")
[1] "00:05:00" "00:20:00" "01:00:00" "01:05:00"
[5] "01:50:00" "10:50:00" "20:05:00" "20:10:00"
sprintf
fills the character values of the time variable hte with leading 0. as.POSIXct
reads the time as the current date with this time, and then the external function format.POSIXt
removes the date characters.
How about this approach?
time <- readLines(n = 8)
5
20
100
105
150
1050
2005
2010
sub("(\\d{2})(\\d{2})", "\\1:\\2:00", sprintf("%04d", as.numeric(time)))
# [1] "00:05:00" "00:20:00" "01:00:00" "01:05:00" "01:50:00" "10:50:00" "20:05:00" "20:10:00"
How about this:
library(stringr)
TIME <- c('5','20','100','105','150','1050','2005','2010')
TIMEpadded <- paste0(str_pad(TIME, width=4, pad=0), '00')
sub('(.+)(.{2})(.{2})', '\\1:\\2:\\3', TIMEpadded)
## [1] "00:05:00" "00:20:00" "01:00:00" "01:05:00" "01:50:00" "10:50:00" "20:05:00" "20:10:00"
public static String transform (String in)
{
while (in.length() < 4)
{
in = "0" + in;
}
in = in.substring(0,2) + ":" + in.substring(2,4) + ":00";
return(in);
}
public static void main (String[] args)
{
for (String s : new String[]{"5", "20", "100", "105", "150", "1050", "2005", "2010"})
{
System.out.println(s + " -> " + transform(s));
}
}
Method conversion: 1) Adds zeros to the time value in the string, until a 4-digit string is obtained. 2) inserts ":" every two characters in the string and the trailing "00".
The main method provides the example you provided with the following output:
5 -> 00:05:00
20 -> 00:20:00
100 -> 01:00:00
105 -> 01:05:00
150 -> 01:50:00
1050 -> 10:50:00
2005 -> 20:05:00
2010 -> 20:10:00