0

I am working with the R programming language. I found this question over here that extracts everything from the RIGHT of the first space:

#https://stackoverflow.com/questions/15895050/using-gsub-to-extract-character-string-before-white-space-in-r

dob <- c("9/9/43 12:00 AM/PM", "9/17/88 12:00 AM/PM", "11/21/48 12:00 AM/PM")

gsub( " .*$", "", dob )
# [1] "9/9/43"   "9/17/88"  "11/21/48"

Is it possible to adapt this code to extract after the first space?

# option 1

12:00 AM/PM, 12:00 AM/PM, 12:00 AM/PM

# option 2 : part 1

 12:00, 12:00 ,  12:00 

# option 2: part 2

AM/PM, AM/PM, AM/PM

# then, concatenate option 2 : part 1 and option 2 : part 2

I thought that maybe switching the syntax of the "gsub" command might accomplish this:

 gsub( "$*. ", "", dob )
 gsub( "*$. ", "", dob )

But I don't think I am doing this correctly.

Can someone please show me how to do this (option 1 and [option 2 part 1, option 2 part 2])?

Thanks!

Note: Normally, I do this with "Text to Columns" in Microsoft Excel - but I would like to learn how to do this in R!

stats_noob
  • 5,401
  • 4
  • 27
  • 83

2 Answers2

2

Do you mean the following?

dob <- c("9/9/43 12:00 AM/PM", "9/17/88 12:00 AM/PM", "11/21/48 12:00 AM/PM", "red1 23 g")

gsub("^\\S+ ", "", dob)

#> [1] "12:00 AM/PM" "12:00 AM/PM" "12:00 AM/PM" "23 g"
PaulS
  • 21,159
  • 2
  • 9
  • 26
  • 1
    @ PaulS : thank you so much! Can your code be used for very general strings? For example c("red1 23 g") ... I would like to return "23 g". Wherever you see the first space, I want everything after the first space. Is this possible with your code? Thanks! – stats_noob Jul 07 '22 at 14:11
  • Yes: Please, see my updated solution. @sindri_baldur's also works perfectly! – PaulS Jul 07 '22 at 14:16
1

Option 1: Remove the first space and everything before it?

sub(".*? ", "", dob)
# "12:00 AM/PM" "12:00 AM/PM" "12:00 AM/PM"

Option 2: Remove the last space and everything before it?

sub(".* ", "", dob)
# [1] "AM/PM" "AM/PM" "AM/PM"

Option 3: Remove the first/last space and everything before/after it?

gsub(" [^ ]+$|^.*? ", "", dob)
# [1] "12:00" "12:00" "12:00"
s_baldur
  • 29,441
  • 4
  • 36
  • 69