简体   繁体   中英

How to split and separate time stamp from a string in R

I have a string and need to separate time stamp from that string. I used strsplit function for string splitting. Attaching my code here.

string <- "10:00:in(XXXX),10:47:out(XXXX),12:36:in(XXXX),13:12:out(XXXX)"
splt <- strsplit(string,split = ",")[[1]]
> splt
[1] "10:00:in(XXXX)"  "10:47:out(XXXX)" "12:36:in(XXXX)"  "13:12:out(XXXX)"

Following is my expected output,

[1] "10:00"  "10:47" "12:36"  "13:12"

What modification should I need to add in strsplit function. Can you help me to figure out this problem ?

Thanks in advance

I'm not sure about what you could add to strsplit but you could wrap it in a substr call and as your times are a standard length this will work.

string <- "10:00:in(XXXX),10:47:out(XXXX),12:36:in(XXXX),13:12:out(XXXX)"
splt <- substr(strsplit(string,split = ",")[[1]], 1, 5)
splt
[1] "10:00" "10:47" "12:36" "13:12" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM