Reputation: 28129
I have a data frame with a column containing semi-structured text. I'm looking for certain pieces of information but they have a pattern I'm not sure how to exploit.
head(events5)
flight_history_id date_time_recorded event
862 280604281 2012-11-13 17:55:58.673-08 Arrival Estimation
5514 280316415 2012-11-12 17:25:08.814-08 Time Adjustment
5527 280315758 2012-11-12 16:10:06.968-08 Time Adjustment
5539 280316495 2012-11-12 15:38:10.099-08 Time Adjustment
5545 280303866 2012-11-13 06:51:45.831-08 Time Adjustment
5558 280303866 2012-11-12 05:50:23.547-08 Time Adjustment
data_updated
862 EGA- Based on Distance and Airspeed
5514 EGD- New=11/13/12 20:20, DGATE- New=A1, EGA- New=11/13/12 21:00
5527 EGD- New=11/13/12 19:05, DGATE- New=A7, EGA- New=11/13/12 20:50, AGATE- New=C5
5539 EGD- New=11/13/12 06:35, DGATE- New=A1, EGA- New=11/13/12 07:15
5545 AGD- Old=11/13/12 07:40 New=11/13/12 07:38, EGA- Old=11/13/12 10:25 New=11/13/12 10:18
5558 EGD- New=11/13/12 07:45, DGATE- New=11, EGA- New=11/13/12 10:25, AGATE- New=A1
here's the dput:
structure(list(flight_history_id = c(280604281L, 280316415L,
280315758L, 280316495L, 280303866L, 280303866L, 280271465L, 280271465L,
280271465L, 280271465L), date_time_recorded = structure(c(10L,
4L, 3L, 2L, 7L, 1L, 9L, 8L, 6L, 5L), .Label = c("2012-11-12 05:50:23.547-08",
"2012-11-12 15:38:10.099-08", "2012-11-12 16:10:06.968-08", "2012-11-12 17:25:08.814-08",
"2012-11-12 17:58:14.268-08", "2012-11-13 05:16:41.01-08", "2012-11-13 06:51:45.831-08",
"2012-11-13 07:38:19.593-08", "2012-11-13 07:54:22.588-08", "2012-11-13 17:55:58.673-08"
), class = "factor"), event = structure(c(1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L), .Label = c("Arrival Estimation", "Time Adjustment"
), class = "factor"), data_updated = structure(c(3L, 8L, 7L,
4L, 2L, 5L, 1L, 10L, 9L, 6L), .Label = c("AGD- New=11/13/12 10:22, EGA- Old=11/13/12 11:20 New=11/13/12 11:09",
"AGD- Old=11/13/12 07:40 New=11/13/12 07:38, EGA- Old=11/13/12 10:25 New=11/13/12 10:18",
"EGA- Based on Distance and Airspeed", "EGD- New=11/13/12 06:35, DGATE- New=A1, EGA- New=11/13/12 07:15",
"EGD- New=11/13/12 07:45, DGATE- New=11, EGA- New=11/13/12 10:25, AGATE- New=A1",
"EGD- New=11/13/12 08:55, EGA- New=11/13/12 09:45, AGATE- New=A1",
"EGD- New=11/13/12 19:05, DGATE- New=A7, EGA- New=11/13/12 20:50, AGATE- New=C5",
"EGD- New=11/13/12 20:20, DGATE- New=A1, EGA- New=11/13/12 21:00",
"EGD- Old=11/13/12 08:55 New=11/13/12 10:05, EGA- Old=11/13/12 09:45 New=11/13/12 10:55",
"EGD- Old=11/13/12 10:05 New=11/13/12 10:30, EGA- Old=11/13/12 10:55 New=11/13/12 11:20"
), class = "factor")), .Names = c("flight_history_id", "date_time_recorded",
"event", "data_updated"), row.names = c(862L, 5514L, 5527L, 5539L,
5545L, 5558L, 5564L, 5566L, 5570L, 5572L), class = "data.frame")
I'm trying to extract the field 'New='
portion of the parts starting with 'EGA'
from the column 'data_updated'
. The problem is sometimes there is an 'Old=' field in between the two. I looked at using stringr::str_split and turning the column into a list, but still have the same issue. I assume this is a regex/gsub issue, but don't know exactly how to implement.
Any suggestions?
EDIT: I'd like the end product to be a vector / column with the EGA/New value. So something like this:
NA
11/13/12 21:00
11/13/12 20:50
11/13/12 07:15
11/13/12 10:18
11/13/12 10:25
Upvotes: 2
Views: 245
Reputation: 193497
Similar in concept to @BlueMagister's but without as much nesting:
gsub(".*EGA-.*New=([0-9/]{8} [0-9:]{5}).*|.*EGA.*", "\\1", events5$data_updated)
# [1] "" "11/13/12 21:00" "11/13/12 20:50" "11/13/12 07:15"
# [5] "11/13/12 10:18" "11/13/12 10:25" "11/13/12 11:09" "11/13/12 11:20"
# [9] "11/13/12 10:55" "11/13/12 09:45"
Basically, the first part of the gsub
says to look for "EGA-", which does not necessarily come at the start of the string, and start capturing when you encounter "New=". The capture pattern ([0-9/]{8} [0-9:]{5})
says you first expect a string with digits and slashes 8 characters long, followed by a space, followed by a string with digits and colons 5 characters long. Then the |
character is used to say also look for EGA with anything before or after it.
To add the new information to your data.frame
you can use something like:
within(events5, {
EGA.New <- gsub(".*EGA-.*New=([0-9/]{8} [0-9:]{5}).*|.*EGA-.*",
"\\1", events5$data_updated)
})
Upvotes: 1
Reputation: 13363
Here's a start-to-end solution:
##formats of the date-times
date.format <- "\\d{2}/\\d{2}/\\d{2} \\d{2}:\\d{2}"
##puts this together into a regex
regex.string <- paste0("EGA- (?:Old=",date.format," )?New=(",date.format,")")
##if pattern matches, get the part of "new" we are looking for
##otherwise NA
ifelse(grepl(regex.string,events5$data_updated),
gsub(paste0("^.*",regex.string,".*$"),"\\1",events5$data_updated),
NA_character_)
Upvotes: 1
Reputation: 3667
How about that?
EGA-\s*(?:Old=\d{2}\/\d{2}\/\d{2}\s+\d{2}:\d{2}\s*)?New=(\d{2}\/\d{2}\/\d{2}\s+\d{2}:\d{2})
Upvotes: 2
Reputation: 2621
Here's an alternative that will compared to @Pharaoh's regex work with an unlimited amount of text after the EGA
-tag
/EGA-(?:(?!New=|").)+(New=[:\d\/ ]+)/g
If the "
in the negative lookahead is too general, you can just change it to say EGD
.
Demo+explanation: http://regex101.com/r/qY4uA0
Remove the flag and delimiters if they are not needed.
Upvotes: 2