How to extract section of string from each string in factor vector in R? -


i have column time stamps (that classed factors @ moment), i'd extract from. there 220,000 rows , half contain factors "%d.%m.%y %h:%m:%s" , other half, "%d.%m.%y %h:%m". half have 3 more charters other half.

i'd extract each row the, %d.%m.%y %h:%m", leaving out :%s contain it.

since they're classed factors, assumption extracted order, i.e. extract 1st - 14th charcter.

this data looks like.

start.timestamp 01.01.2015 12:32 01.01.2015 08:22 01.01.2015 14:10 31.12.2014 21:43 01.01.2015 00:21 01.01.2015 12:38 01.01.2015 01:00 01.01.2015 14:13 01.01.2016 04:11:34 01.01.2016 10:13:30 01.01.2016 04:30:08 01.01.2016 08:49:40 01.01.2016 07:44:45 

also - ":00" factors strings missing "%s" acceptable.

i hope clear.

thank in advance

depending on initial data is, like:

lapply(df,substring, first=1, last=16) 

could maybe help.

when data this:

df <- data.frame("start.timestamp",              "01.01.2015 12:32",              "01.01.2015 08:22",              "01.01.2015 14:10",              "31.12.2014 21:43",              "01.01.2015 00:21",              "01.01.2015 12:38",              "01.01.2015 01:00",              "01.01.2015 14:13",              "01.01.2016 04:11:34",              "01.01.2016 10:13:30",              "01.01.2016 04:30:08",              "01.01.2016 08:49:40",              "01.01.2016 07:44:45")  lapply(df,substring, first=1, last=16) #$x.start.timestamp. #[1] "start.timestamp"  #$x.01.01.2015.12.32. #[1] "01.01.2015 12:32"  #$x.01.01.2015.08.22. #[1] "01.01.2015 08:22"  #$x.01.01.2015.14.10. #[1] "01.01.2015 14:10"  #$x.31.12.2014.21.43. #[1] "31.12.2014 21:43"  ... 

or other of apply functions, not know how whole data set up.


Comments