Using gsub to extract character string before white space in R -
i have list of birthdays this:
dob <- c("9/9/43 12:00 am/pm", "9/17/88 12:00 am/pm", "11/21/48 12:00 am/pm")
i want grab calendar date variable (ie drop after first occurrence of white-space).
here's have tried far:
dob.abridged <- substring(dob,1,8) dob [1] "9/9/43 1" "9/17/88 " "11/21/48" dob.abridged <- gsub(" $","", dob.abridged, perl=t) > dob.abridged [1] "9/9/43 1" "9/17/88" "11/21/48"
so code works calendar dates of length 6 or 7, not length 8. pointers on more effective regex use gsub can handle calendar dates of length 6, 7 or 8?
thank you.
gsub( " .*$", "", dob ) # [1] "9/9/43" "9/17/88" "11/21/48"
a space (), character (
.
) number of times (*
) until end of string ($
). see ?regex learn regular expressions.
Comments
Post a Comment