0

I am working with the following column string in a dataframe

s <- "[08/26/2021 06:58:12 260] - MacId: 40_E3_D6_CA_56_5C RSSI: -92"

I would like to split that somehow into the following columns

  • df$Datetime = 08/26/2021 06:58:12 260
  • df$MacId = 40_E3_D6_CA_56_5C
  • df$RSSI = -92

Is there a function which can do so?

Glenn
  • 17
  • 7

1 Answers1

3

You may use strcapture and pass the pattern to extract the data required.

s <- "[08/26/2021 06:58:12 260] - MacId: 40_E3_D6_CA_56_5C RSSI: -92"

result <- strcapture('\\[(.*)\\] - MacId: (.*) RSSI: (.*)', s, 
           proto = list(Datetime = character(), MacId = character(), 
                        RSSI = numeric()))

result
#                 Datetime             MacId RSSI
#1 08/26/2021 06:58:12 260 40_E3_D6_CA_56_5C  -92

If the string is present in a dataframe the same regex pattern can be used in tidyr::extract -

tidyr::extract(data.frame(s), s, c('Datetime', 'MacId', 'RSSI'), 
               "\\[(.*)\\] - MacId: (.*) RSSI: (.*)")
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213